對于一個學校專案,我和我的小組伙伴制作了一個代碼,我在一個單獨的測驗檔案中測驗了每個功能,看看它們是否有效并且看起來都不錯,但是選單沒有按預期作業。我的大腦似乎無法弄清楚我哪里出錯了,我很想對此提出第二意見..非常感謝 <3 這是我的代碼!
def mainmenu():
print("Hello! Welcome to The Sprint Project Company! Please choose from 1-5: ")
print("1. Simple IPO Program")
print("2. IFs and Loop sample")
print("3. Strings and dates")
print("4. Data files and Default Values")
print("5. Quit")
while True:
choice = input("Enter choice (1-5): ")
if choice == 1:
ipo()
elif choice == 2:
ifloop()
elif choice == 3:
stringsdates()
elif choice == 4:
datafiles()
else:
break
mainmenu()
每當我運行它時,它都會自動結束。我什至通過在 else 下放置一個列印部分來進行測驗,但它只是直接跳到結束代碼。非常感謝您看我的問題 <3
uj5u.com熱心網友回復:
您的代碼有兩點。首先,函式“input()”回傳一個字串,因此您將字串與整數進行比較,然后它的值為假。這就像您將 1 與 '1' 進行比較,而它們并不相同。
其次,您的函式 mainmenu() 必須放在回圈中。進行這兩個更改,它將起作用。
while True:
mainmenu() # Add the function here.
choice = int(input("Enter choice (1-5): ")) # Change here
if choice == 1:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/365318.html
標籤:Python if 语句 while 循环 菜单 用户定义函数
上一篇:嘗試未決議的json字串,但獲得了物件“{”的預期開始,但改為“EOF”
下一篇:將一個數除以百分之一
