Simulating Random Dice Rolls
00:00 In this lesson, you’ll be implementing the logic for simulating random dice rolls. Here in your app’s main code block, add a comment for step two, roll the dice.
00:10
roll_results = roll_dice(
taking in the argument num_dice)
And for this lesson, you’ll also want to test displaying the results.
00:18
You’ll remove the test code later. print(roll_results)
, of course, the roll_dice()
function hasn’t been defined yet, so let’s change that. Place roll_dice()
after parse_input()
and before the app’s main code block: def roll_dice
taking in the parameter (num_dice):
00:38
and a docstring describes its functionality. Return a list of integers with length num_dice
.
00:46
Each integer in the returned list is a random number between 1
and 6
inclusive, meaning 1
and 6
are both possible values to be returned.
00:56
Once again, you can pause here and try writing the rest of the function yourself or follow along with me. Hint, you will need Python’s random
module, and that’s where we’ll start.
01:06
import random
at the top of the file,
01:13
and back in the roll_dice()
function, create a variable roll_results
containing an empty list. And for _ in range(num_dice):
01:24
roll = random.randint(
taking in the integer arguments 1
and 6
, roll_results.append(roll)`
01:33
and return roll_results
.
01:36
So first you have this empty list roll_results
. Then, you use a for
loop to iterate over a range object of the size num_dice
.
01:44
This will run the subsequent block of code num_dice
times, one for each die. Oh, and using an underscore here is a common Python idiom to indicate that the variable won’t be used within the loop.
01:55
And within the loop, roll
takes on the result of calling random.randint()
with the arguments 1
and 6
.
02:02
What’s that? What’s randint
and what do these arguments mean? I’m glad you asked. The randint()
function takes two arguments, the upper and lower limits of an integer range.
02:12 It then returns a random integer from anywhere within that range, upper and lower limits included, with every integer having equal likelihood of being returned.
02:20 Technically, this kind of random function is considered pseudorandom because there is a deterministic algorithm behind the function producing the numbers.
02:30
But the results are random enough for our purposes. Then, the roll that was returned is appended to roll_results
using the .append()
method of lists.
02:39 Finally, the list of results is returned.
02:44
And since you’ve already written the code to call this function and display the results, all that’s left to do is try it out. Save, open the terminal, python dice.py
.
02:55 Same prompt as before. How many dice do you want to roll? I’ll say five. And there it is. I get five dice as a result. Because this is random, your results are most likely going to differ.
03:08 Great. Everything is working as planned. You’re really on a roll. I think it’s time we go double or nothing. Over the next two lessons, you’ll write the code to finally generate the dice diagrams and fully implement the app.
Become a Member to join the conversation.