出於某種原因,我的程序不管用戶的回答都會再次擲骰子,並且不會打印再見消息。爲什麼我的程序會這樣做?
我
from random import randint
def rollthedice():
print("The number you rolled was: "+ str(randint(1,6)))
rollthedice()
shouldwecontinue = input("Do you wish to roll again? (y or n) ").lower()
if shouldwecontinue == "y" or "yes":
rollthedice()
elif shouldwecontinue == "n" or "no":
print("Goodbye.")
elif shouldwecontinue != type(str):
print("Sorry the program only accepts 'y' or 'n' as a response.")
程序應該是,如果用戶輸入Y,它滾動的骰子的期望的效果再次,包括下面的代碼,以便可以測試自己(3 Python)的開始分別整個程序再次。而如果用戶輸入「否」,則應打印「再見」消息。
我們幾乎每天都會看到這個錯誤,這很常見。使用'if should we continue in(「y」,「yes」):'代替。 –
@PaulRooney - 幾乎每一天?更像每天多次。 >。> – TigerhawkT3
而且,OP,您可能會考慮一個問題標題,它在技術上不適用於網站上的每個問題。如果您正在閱讀手冊的疑難解答部分,那麼如果目錄中有20個相同的條目,標記爲「出現問題並且您想要解決問題時」,會有多大幫助? – TigerhawkT3