继续循环外的骰子滚动脚本



我正在做一个掷骰子游戏。下面的代码产生一个错误,说";在循环外继续":

import random
repeat="y" or "yes"
while repeat == "y" or "yes"
print("Rolling the dice")
print(random.randint(1,6))
repeat = input("Do you wanna roll again Y/N?").lower()
if repeat =="y" or "yes":
continue
else:
print("Thanks for rolling")
break

为什么我会出现此错误?

您不能同时影响两个墙,请选择"y";或";Y";在你的表情中。

repeat="y"
while (repeat == "y") or (repeat == "yes"):

你也必须检查你的压痕,repeat=input必须在循环中,所以每次都要检查一个新的输入。最后的打印可以放在循环的最后。

这是一个工作示例,您可以键入任何以"开头的内容;y"/"Y";继续滚动:

import random
repeat="y"
while (repeat[0]) == "y":
print("Rolling the dice")
print(random.randint(1,6))
repeat = input("Do you wanna roll again Y/N?").lower()
print("Thanks for rolling")

最新更新