【发布时间】:2020-05-02 12:35:32
【问题描述】:
我正在尝试做这个项目,但我迷路了。这是我第一次做这样的程序,我不知道从哪里开始。我试图让 dice.roll = 1 到 6 的范围,但我认为我不应该这样做,因为它说名称“random”没有定义。我不知道我做错了什么,也没有人可以解释。
# ************************
# Step 1: import from dice the Dice class
from dice import Dice
# Step 2: For the variable name dice, assign the Dice() class
class Dice():
results=[]
# Step 3: Declare an empty results list
results=[]
# Step 4: Roll the dice 1000 times
for roll_num in range(1000):
dice.roll= random.randint(1,6)
# stores the results for each single dice roll in a list
result = dice.roll()
results.append(result)
# analyze the results
frequencies = []
# range starts at 0, increments by 1 and stops just before the last number
# Uses +1 to stop just before 7 for 1-6 sides
for value in range(1, dice.num_sides +1):
# Step 5: call the value as you count the results
frequency = results.count()
# Step 6: similar to the results list, keep appending or adding the frequency
frequencies.append()
# Step 7: Print the frequencies to the Python Shell
print()
【问题讨论】:
-
random是一个必须导入的模块。你确定你已经导入了依赖? (即import random) -
我的老师让我们为它导入这个程序。我试图弄清楚如何让骰子滚动 1000 次,但我不明白如何。 from random import randint class Dice(): """一个六面骰子的类""" def __init__(self, dice_sides=6): """六面骰子。""" self.dice_sides = dice_sides def roll(self): """返回一个介于 1 和边数之间的随机值。""" return randint(1, self.dice_sides)
-
这能回答你的问题吗? Python dice simulation
-
google.com/search?q=python+dice周围有很多人做同样的作业。
标签: python