第一次發帖!
在選單上鍵入“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("-------------------------------------------------------------------------------------------------------")
希望格式正確。謝謝!!
uj5u.com熱心網友回復:
您的 while 回圈取決于引數mainmenu:
while mainmenu != "x":
但是你得到x變數的輸入menu:
menu = input("\n""Type number of category: " "\n")
uj5u.com熱心網友回復:
您將值存盤在選單中,而 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("-------------------------------------------------------------------------------------------------------")
uj5u.com熱心網友回復:
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("-------------------------------------------------------------------------------------------------------")
uj5u.com熱心網友回復:
嘗試將前兩行更改為:
menu = ""
while menu != "x":
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/482253.html
標籤:Python python-3.x python-2.7 while循环
上一篇:從Python2.7遷移到3.7-isinstance(obj,None)與isNone之間的區別
下一篇:dict中的python選擇鍵
