![](/img/trans.png)
[英]Can not figure out why my if/else statement doesn`t work like i intend
[英]Can't figure out why my else statement is still calling this function with negative input
我正在自學 Python 並決定制作一個基於文本的簡短游戲,首先我決定制作一個 function 供用戶確認他們實際上確實想用“是或否”選項玩游戲,但是,即使輸入“否”,仍然會調用游戲 function。 任何幫助將不勝感激。
這是我進入游戲的命令提示符:
while i <= 10000000000000000:
command = input(":")
if command == "List commands":
command_list()
i += 1
elif command == "Atlas":
atlas_confirmation()
i += 1
這是確認用戶想要玩游戲的提示:
def atlas_confirmation():
print("Alright, but this one's pretty spooky. Are you sure?")
yn = input(":")
if yn == "Yes" or "yes" or "I'm sure" or "Im sure" or "im sure" or "i'm sure":
atlas_game()
elif yn == "No" or "no":
print(command_end)
這是游戲的占位符:
def atlas_game():
print(placeholder)
print(command_end)
當給定正輸入時,我得到所需的 output :
Uh oh! Looks like this code has yet to be completed, but it will be available soon!
Is there anything else I can do for you?
:
但是,當我給出負輸入時,output 仍然與上面相同 ^ 就像我給出了正輸入一樣,而不是所需的 output:
Is there anything else I can do for you?
:
我希望我的“如果”陳述在某個地方是錯誤的,但我不知道在哪里或為什么。
任何幫助將不勝感激,
謝謝你。
我認為您需要從atlas_confirmation()
function 返回command_end
以檢查 while 循環中的中斷條件。 將您的atlas_confirmation()
function 更新為:
def atlas_confirmation():
print("Alright, but this one's pretty spooky. Are you sure?")
yn = input(":")
if yn == "Yes" or yn == "yes" or yn == "I'm sure" or yn == "Im sure" or yn == "im sure" or yn == "i'm sure":
atlas_game()
elif yn == "No" or yn == "no":
return command_end
而你的while循環到這個:
while i <= 10000000000000000:
command = input(":")
if command == "List commands":
command_list()
i += 1
elif command == "Atlas":
atlas_confirmation()
i += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.