Python - While 循环未结束

Python - While Loop not ending

第一次发帖!

我的程序在菜单中输入“X”后没有结束。似乎 While 没有中断并继续循环。除了退出功能外,该程序可以正常工作。目标是键入 x(小写或大写)并结束程序。

有什么想法吗?感谢编程传奇!

#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("-------------------------------------------------------------------------------------------------------")

希望格式正确。谢谢!!

mainmenu 永远不会等于 x,因此循环将始终重复。您只需要添加:

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

下面

variable 菜单的每个实例更新为 mainmenu

前者是更快的解决方法,后者是我建议的可读性。

#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("-------------------------------------------------------------------------------------------------------")

尝试将前两行更改为:

menu = ""

while menu != "x":

您的 while 循环取决于参数 mainmenu:

while mainmenu != "x":

但是你得到 x 作为变量 menu:

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

您正在菜单中存储值,而 while 循环条件是变量 mainmenu != "x:,因此 while 循环不会停止。

# 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("-------------------------------------------------------------------------------------------------------")