出于某种原因,我的程序不管用户的回答都会再次掷骰子,并且不会打印再见消息。为什么我的程序会这样做?
我
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