How to Roll a Dice Using Python
by amna.siddiqui008 in Circuits > Computers
15406 Views, 1 Favorites, 0 Comments
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
Supplies
Some Experience with Python
IDEL for Python Code (Python 3.0 or higher
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
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.
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
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.
Create the String and Python Representation
Now create a function to get the string and python representation of the die
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
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
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
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: https://www.python.org/about/gettingstarted/. The official Python website will provide you a more in-depth explanation on coding alongside more examples to follow.