0

I am trying to do this project but I am lost. This is the first time I have done a program like this and I don't know where to start. I tried to have the dice.roll = a range of 1 to 6, but I think I was not supposed to since it says the name "random" is not defined. I have no idea what I am doing incorrectly and do not have anyone available to explain.


# ************************

# 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()
TheBot
  • 1
  • 1
  • 1
    `random` is a module that has to be imported. Are you sure you have imported the dependency? (i.e. `import random`) – Jake Tae May 02 '20 at 03:01
  • My teacher is having us import this program for it. I am trying to figure out how to get the die to roll a 1000 times and am not understanding how. from random import randint class Dice(): """A class for a single six-sided die""" def __init__(self, dice_sides=6): """A six-sided die.""" self.dice_sides = dice_sides def roll(self): """Returns a random value between 1 and the number of sides.""" return randint(1, self.dice_sides) – TheBot May 02 '20 at 03:04
  • Does this answer your question? [Python dice simulation](https://stackoverflow.com/questions/12771961/python-dice-simulation) – Joe May 02 '20 at 04:42
  • https://www.google.com/search?q=python+dice There are many many people around who had the same homework. – Joe May 02 '20 at 04:43
  • https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend – Joe May 02 '20 at 04:43

1 Answers1

0

Honestly, I feel OP (original poster) lacks understanding for several python concepts.

  1. import in the module

I believe you have a python file called dice.py, containing the definitions of the Dice class, probably as follows (from your comments)

from random import randint
class Dice():
    """A class for a single six-sided die"""
    def __init__(self, dice_sides=6):
        """A six-sided die."""
        self.dice_sides = dice_sides

    def roll(self):
        """Returns a random value between 1 and the number of sides."""
        return randint(1, self.dice_sides)

You should know that a function randint in random is imported in the python file dice.py, and then one could use the functions in the file, like randint(1, self.dice_sides).

(the return value of Dice.roll() should be randint(1, self.dice_sides+1)

  1. instance of a class

After you import Dice from the module dice, you could instantiate the class like below. This corrects your code in Step 2.

adice = Dice()
# then this would be a 6-side dice

Then you could get a roll number from the variable, as it has method (that is, function in class) roll

a_random_result = adice.roll()
print(a_random_result)

After this, you should get use a for cycle to get 1000 results and store them in results.

liginity
  • 311
  • 3
  • 7
  • If you think he `lacks understanding for several python concepts` better post a link to a tutorial in the comments or find some duplicate question. – Joe May 02 '20 at 04:41
  • stackoverflow is (in my understanding) not supposed to be a custom-tutorial-collection, where people are teaching other people the most basic language skills over and over. – Joe May 02 '20 at 04:46
  • thanks, I could read the guide line for asking and answering questions and get more experience for this. – liginity May 02 '20 at 04:58