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

(vip2019) #1

136 Chapter 6


Figure 6-7: The mirrored/repeated effect in Kaleidoscope.py.

What You Learned


Before this chapter, we had no way of making a computer behave
randomly. Now we can make a computer roll dice; draw random
cards from a deck; draw spirals of random color, shape, size, and
location; and even beat us now and then at Rock-Paper-Scissors.
The tool that made these programs possible was the random
module. We used random.randint(1, 10) to generate a random
number between 1 and 10 in our guessing game. We added the
random.choice() function to pick a random color out of a list in our
random spirals program. You learned how to use the functions
turtle.window_width() and turtle.window_height() to find the width
and height of our turtle screen.
You also learned how to use Cartesian coordinates to find an
(x, y) location on the screen, and you used the random.randrange()
function to generate a number in the range between our left and
right x-coordinate values and top and bottom y-coordinate values.
We then used turtle.setpos(x,y) to move the turtle to any position
on the drawing screen.
Free download pdf