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
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()
:
def roll_die():
x = randint(1, 6)
print(x)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.