Solution Design First, create a variable to track the running total of the dice rolls and start it with the value 0.
Then make a for loop to loop the same number of times as the numberOfDice parameter. Inside
the loop, call the random.randint() function to return a random number between 1 and 6 and
add this number to the running total. After the loop completes, return the total.
If you don’t know how the random.randint() function works, you can run import
random
and help(random.randint) in the interactive shell to view its documentation. Or you
can do an internet search for ―python randint‖ to find information about the function online.
Special Cases and Gotchas There are no special cases for this exercise, but remember that Python’s random.randint()
function is inclusive of its arguments. Calling, for example, range(6) causes a for loop to loop
from 0 up to, but not including, 6. But calling random.randint(1, 6) returns a random number
between 1 and 6, including the 1 and the 6. If you roll three 6-sided dice, the range is from 3 to 18,
not 1 to 18.
Rolling multiple dice doesn’t produce a uniform distribution: rolling two 6-sided dice is much
more likely to come up with 7 because there are more combinations that add up to 7 (1 + 6, 2 + 5, 3
+ 4, 4 + 3, 5 + 2, and 6 + 1) compared with rolling 2 (1 and 1 only). This is why you would need to
call random.randint(1, 6) twice and add the rolls together instead of call
random.randint(2, 12)
once.
Your solution program needs import random at the top of the program in order to call the
random.randint()
function, or else you’ll get a NameError: name 'random' is not
defined
error message.)
Now try to write a solution based on the information in the previous sections. If you still have
trouble solving this exercise, read the Solution Template section for additional hints.