Home > OS >  Python - While Loop not ending
Python - While Loop not ending

Time:06-09

first time posting!

My program is not ending after typing "X" at the menu. It seems While is not breaking and continuing to loop. The program works properly other than the exit feature. Goal is to type x (lower or upper case) and end program.

Any ideas? Thanks Programming Legends!

#initial display

print()
print("Welcome to The Survival App!")
print()
input("Press enter to continue")

#category

mainmenu = ""

while mainmenu != "x":
print()
print("Main Menu")
print("Type Category Number or Type X to EXIT!")
print("1 - Navigation")
menu = input("\n""Type number of category: " "\n")


    if menu == "1":
    print("Where is the North Star located?")
    input("Press enter to show possible answers...""\n")
    print("A) Tail of the Little Dipper.")
    print("B) The brightest star in the sky.")
    print("C) The Tail of the Big Dipper.")

#answer
    answer = input("\n" "Type A, B, or C: ").lower()

    if answer == "a":
        print("---------------------------------------------------------------------------------------------------")
        print("\n" "Congrats, you didn't get lost!" "\n")
        print("---------------------------------------------------------------------------------------------------")

    elif answer == "b":
        print("---------------------------------------------------------------------------------------------------")
        print("\n" "Sadly, you got lost.")
        print("The North Star can be located on the tail of the Little Dipper!" "\n")
        print("---------------------------------------------------------------------------------------------------")

    elif answer == "c":
        print("---------------------------------------------------------------------------------------------------")
        print("\n" "Sadly, you got lost.")
        print("The North Star can be located on the tail of the Little Dipper!" "\n")
        print("---------------------------------------------------------------------------------------------------")


    else:
        print("---------------------------------------------------------------------------------------------------")
        print("\n" "Sorry, only available options are A, B, and C!" "\n")
        print("---------------------------------------------------------------------------------------------------")

#EXIT MAIN MENU
elif menu == "x":
    print("Good Luck! Don't Die!")

else:

    print("-------------------------------------------------------------------------------------------------------")
    print("Sorry, that isn't an option!")
    print("-------------------------------------------------------------------------------------------------------")

Hopefully that formatted correctly. Thanks!!

CodePudding user response:

Your while loop depends on the param mainmenu:

while mainmenu != "x":

But you get x as the input for variable menu:

menu = input("\n""Type number of category: " "\n")

CodePudding user response:

You are storing value in menu, and while loop condition is with variable mainmenu != "x:, hence while loop is not stopping.

# initial display

print()
print("Welcome to The Survival App!")
print()
input("Press enter to continue")

# category

mainmenu = ""

while mainmenu != "x":
    print()
    print("Main mainmenu")
    print("Type Category Number or Type X to EXIT!")
    print("1 - Navigation")
    mainmenu = input("\n""Type number of category: " "\n")

    if mainmenu == "1":
        print("Where is the North Star located?")
        input("Press enter to show possible answers...""\n")
        print("A) Tail of the Little Dipper.")
        print("B) The brightest star in the sky.")
        print("C) The Tail of the Big Dipper.")

# answer
        answer = input("\n" "Type A, B, or C: ").lower()

        if answer == "a":
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Congrats, you didn't get lost!" "\n")
            print("---------------------------------------------------------------------------------------------------")

        elif answer == "b":
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Sadly, you got lost.")
            print("The North Star can be located on the tail of the Little Dipper!" "\n")
            print("---------------------------------------------------------------------------------------------------")

        elif answer == "c":
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Sadly, you got lost.")
            print("The North Star can be located on the tail of the Little Dipper!" "\n")
            print("---------------------------------------------------------------------------------------------------")

        else:
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Sorry, only available options are A, B, and C!" "\n")
            print("---------------------------------------------------------------------------------------------------")

# EXIT MAIN mainmenu
    elif mainmenu == "x":
        print("Good Luck! Don't Die!")

    else:

        print("-------------------------------------------------------------------------------------------------------")
        print("Sorry, that isn't an option!")
        print("-------------------------------------------------------------------------------------------------------")

CodePudding user response:

mainmenu is never equal to x so the loop will always repeat. You just need to add either:

mainmenu = 'x' underneath "good luck don't die"

or

Update each instance of the variable menu to mainmenu

The former is the quicker way to solve it, the latter is what I would suggest for readability.

#initial display

print()
print("Welcome to The Survival App!")
print()
input("Press enter to continue")

#category

menu = ""

while menu != "x":
    print()
    print("Main Menu")
    print("Type Category Number or Type X to EXIT!")
    print("1 - Navigation")
    menu = input("\n""Type number of category: " "\n")


    if menu == "1":
        print("Where is the North Star located?")
        input("Press enter to show possible answers...""\n")
        print("A) Tail of the Little Dipper.")
        print("B) The brightest star in the sky.")
        print("C) The Tail of the Big Dipper.")

        #answer
        answer = input("\n" "Type A, B, or C: ").lower()

        if answer == "a":
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Congrats, you didn't get lost!" "\n")
            print("---------------------------------------------------------------------------------------------------")

        elif answer == "b":
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Sadly, you got lost.")
            print("The North Star can be located on the tail of the Little Dipper!" "\n")
            print("---------------------------------------------------------------------------------------------------")

        elif answer == "c":
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Sadly, you got lost.")
            print("The North Star can be located on the tail of the Little Dipper!" "\n")
            print("---------------------------------------------------------------------------------------------------")


        else:
            print("---------------------------------------------------------------------------------------------------")
            print("\n" "Sorry, only available options are A, B, and C!" "\n")
            print("---------------------------------------------------------------------------------------------------")

    #EXIT MAIN MENU
    elif menu == "x":
        print("Good Luck! Don't Die!")

    else:

        print("-------------------------------------------------------------------------------------------------------")
        print("Sorry, that isn't an option!")
        print("-------------------------------------------------------------------------------------------------------")

CodePudding user response:

Try to change your first two lines to this:

menu = ""

while menu != "x":
  • Related