How to Roll a Dice Using Python

Introduction: How to Roll a Dice Using Python

The following instructions will guide you on how to create a Python module for “rolling” a dice. This module will generate random numbers from this computerized dice. I will provide functions needed to create the module and provide a description as to what each function will be used for. Learning the basic functions for Python can be overwhelming. This will be a creative way of using Python to allow you to become more familiar using the program by following the given example. After this, you should be able to create functions on your own!

Duration: 3-5 minutes


Some Experience with Python

IDEL for Python Code (Python 3.0 or higher

Step 1: Import Random Module

First, open an IDLE file. Once it’s open, import the random module. We create an order object named “Die.”

*Within this order object, we will have multiple functions

Step 2: Create Initial Function for the Dice

After the colon, press enter and follow proper indentations. Now we will create the initial function for the dice and default the 6 sides. “Self” will call the sides of the dice through the function.

Step 3: Return the Roll Using Radint Method

Next, use the “get” and “roll” functions to generate/return the roll using randint method. Create a variable named self.roll_count to make the random roll inside the “roll” function Return the number from the random roll inside the “get” function

*Randint creates a random number from 1 to self.sides which was 6

Step 4: Create a Function to Get the Number of Sides

Create a function to get the number of sides of the die and return it. This will set the number of sides the dice has and return of the side's number.

Step 5: Create the String and Python Representation

Now create a function to get the string and python representation of the die

Step 6: Create a Separate "rollDice" Function

Now create a new, separate function for “rollDice” to call the order Class Die above

D1 and D2 will be the first roll and second roll

*Notice how there is no indentation in the beginning of this function
*Rolls represent the count of amount of times rolled, and sides will be any integer 1-6

Step 7: Create a Loop Function Within "rollDice"

Next, create a while loop within ‘rollDice’ that will roll the dice however many times when the count is less than or equal to the number of rolls. Be sure to add the print function to generate results.

*Use roll function to generate the roll of the two Die

Step 8: Run Module

Now that we have created the functions for each step, compare with the visual provided to ensure everything is written correctly. We will now run the module. In the menu bar, click Run > Run Module

Step 9: Enter RollDice Function and Have Fun!

Finally, create a rollDice function by entering number of rolls and sides you want.

Hit ‘enter’ to return the results

Congratulations! Now that you have created the module, you are ready use this function to play any board game using Python! You should now be a little more familiar on how the Python language works and use your own creativity to create various modules moving forward.

For more information on Python, visit the website: The official Python website will provide you a more in-depth explanation on coding alongside more examples to follow.

Be the First to Share


    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Make It Bridge

      Make It Bridge
    • For the Home Contest

      For the Home Contest


    Elaina M
    Elaina M

    2 years ago

    Looks really well documented - congrats on posting your first instructable !!