我尝试制作一个简单的骰子滚动程序,我可以让骰子“滚动”,但我无法阻止它,只有当您输入任何内容时它才会保留。我尝试添加它,因此如果您说“否”或“n”,它将返回并像往常一样转到我的其他功能。我说返回是因为继续我的其他代码很重要。谢谢。
import random
min = 1
def rolling6():
roll_more = "yes"
while roll_more == "yes" or roll_more =="y":
max = 6
print("Rolling the dices...")
print("The values are...")
print(random.randint(min, max))
print(random.randint(min, max))
roll = input("Roll the die again? ")
if roll_more == "no" or roll_more == "n":
return
#roll_again = "yes"
#while roll_again == "yes" or roll_again =="y":
# print("Rolling the dices...")
# print("The values are...")
# print(random.randint(min, 6))
# print(random.randint(min, 6))
# roll_again = input("Roll the die again? ")
#if roll_again == "no" or roll_again == "n":
# return
x = input("Use six sided die? ")
while x == "yes" or x =="y":
rolling6()
您正在写入 roll
而不是 roll_more
,因此您始终会检查常量“yes”。
此外,您的 x
和 roll_more
变量不包含您期望的值。首先,输入“yes”进入循环,以便调用 rolling6
。然后,进入rolling6循环。当您通过输入“no”退出时,您退出到外循环,读取仍然具有值“yes”的 x
(因为您从未在任何地方覆盖它),这意味着您不会跳出该循环,并且会再次返回到 rolling6
。
您可能希望将 roll
更改为 roll_more
并将 while x == "yes"or x ==="y":
更改为如果 x =="is"或 x ==“y”:
。
此外,您的 if roll_more == "no"or roll_more == "n": return
是多余的,因为该语句之后没有代码。
我是一名优秀的程序员,十分优秀!