它一直在循环中搞砸它要么什么都不做,一遍又一遍地执行命令,或者执行两次?这些只是我尝试自己做时发生的事情
reaping 一个 if 语句,添加一段时间,找到一个我认为会有帮助的随机相关的东西。 代码 随机导入 导入时间
ans=input("Do you roll a dice? Y/N ")
if ans in ("N", "n"):
print("Then when did you click this game?")
else:
dr=random.choice("123456")
time.sleep(1.7)
print("You got a "+dr)
again = input("Do you want to roll again? Y/N ")
while again in ("y", "Y"):
dr=random.choice("123456")
time.sleep(1.7)`
print("You got a "+dr)
again = input("Do you want to roll again? Y/N ")
看起来你的代码有一些缩进问题。 'print("You got a"+dr)' 语句在 while 循环之外,因此无论用户输入如何,它都只会运行一次。正确的版本看起来像这样:
import random
import time
while True:
ans = input("Do you want to roll a dice? Y/N ")
if ans in ("N", "n"):
print("Then when did you click this game?")
break
else:
dr = random.choice("123456")
time.sleep(1.7)
print("You got a "+dr)
again = input("Do you want to roll again? Y/N ")
if again not in ("y", "Y"):
break