簡體   English   中英

使用 randint 和 while 循環模擬擲骰子

[英]Using randint and while loop to simulate dice 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

我希望卷是隨機的,但無論第一個卷是什么(即 4),都是 while 循環中的每個答案

4
4
4
4
4
4
4
4
4
4

你只“滾動”一次。 每次調用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM