[英]Using randint and while loop to simulate dice roll
I am creating a simple random dice roll but the code prints the same answer for each roll我正在创建一个简单的随机骰子卷,但代码为每个骰子打印相同的答案
from random import randint
x = randint(1, 6)
class Die():
def __init__(self, sides):
self.sides = 6
def roll_die(self):
print(x)
die_1 = Die(6)
roll_count = 0
while roll_count <= 10:
die_1.roll_die()
roll_count += 1
I expect the rolls to be randomized but whatever the first roll is (ie 4), is each answer in the while loop我希望卷是随机的,但无论第一个卷是什么(即 4),都是 while 循环中的每个答案
4
4
4
4
4
4
4
4
4
4
You only "roll" once.你只“滚动”一次。 You want to use
x = randint(1, 6)
each time you call roll_die()
:每次调用
roll_die()
时都想使用x = randint(1, 6)
roll_die()
:
def roll_die():
x = randint(1, 6)
print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.