對於一個學校項目,我和我的小組夥伴製作了一個代碼,我在一個單獨的測試文件中測試了每個功能,看看它們是否有效並且看起來都不錯,但是菜單沒有按預期工作。我的大腦似乎無法弄清楚我哪裡出錯了,我很想對此提出第二意見..非常感謝 <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
您的代碼有兩點。首先,函數“input()”返回一個字符串,因此您將字符串與整數進行比較,然後它的值為假。這就像您將 1 與 '1' 進行比較,而它們並不相同。
其次,您的函數 mainmenu() 必須放在循環中。進行這兩個更改,它將起作用。
while True:
mainmenu() # Add the function here.
choice = int(input("Enter choice (1-5): ")) # Change here
if choice == 1:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句