Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – While Loop not ending

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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!!

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading