Home > Software engineering >  Python - While Loop not ending
Python - While Loop not ending

Time:05-22

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:

mainmenu is never equal to x. Just add mainmenu = x underneath "good luck don't die"

CodePudding user response:

Try to change your first two lines to this:

menu = ""

while menu != "x":

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("-------------------------------------------------------------------------------------------------------")
  • Related