Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

130 Chapter 6


After we import the random module and start the game loop
with a while statement, the next few lines deserve a little explana-
tion. At u, we set up an array called dice that holds five values,
and we initialize all those values to zero. The square brackets,
[ and ], are the same ones we used for our very first lists of colors,
as well as for the arrays of card face values and suit names ear-
lier in this chapter. At v, we set up a for loop to run five times
for the five dice, using the range from 0 to 4; these will be the
array positions, or index numbers, of the five dice.
At w, we set each individual die, from dice[0] to dice[4], equal
to a random integer from 1 to 6 to represent our five dice and their
randomly rolled values. At x, we show the user what dice they
rolled by printing the contents of the dice array; the result of this
print statement is shown in Figure 6-5.

Figure 6-5: A sample run of our dice program. Notice that
we rolled several three of a kinds and one four of a kind.
Free download pdf