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

(vip2019) #1

138 Chapter 6


•    Use random.choice() to pick a value at random out of a list or
a r ray.
• Use random.choice() to generate 52 card values from two arrays
of strings containing only the faces and suits.
• Determine the size of your drawing window with turtle.window_
width() and turtle.window_height().
• Move the turtle to any position on the drawing screen with
turtle.setpos(x,y).
• Use the random.randrange() function to generate a random
number in any range.
• Find the index of an element in a list or array with the .index()
function.
• Build a while game loop using a Boolean flag variable like
keep_going.
• Construct an array of similar types of values, assign values to
elements in the array by their index (as in dice[0] = 2), and use
array elements like regular variables.
• Sort lists or arrays with the .sort() function.
• Reflect points about the x- and y-axes by changing the signs of
the points’ (x, y) coordinate values.
• Change the turtle’s drawing speed with the .speed() function.

Programming Challenges


For this chapter’s challenge problems, we’ll extend the
Kaleidoscope.py and HighCard.py programs. (If you get
stuck, go to http://www.nostarch.com/teachkids/ for
sample answers.)

#1: Random Sides and Thickness


Add more randomness to Kaleidoscope.py by adding two
more random variables. Add a variable sides for the number
of sides and then use that variable to change the angle we
turn each time in the spiral loop (and therefore, the number
of sides in the spiral) by using 360/sides + 1 as your angle
instead of 91. Next, create a variable called thick that will
store a random number between 1 and 6 for the turtle pen’s
Free download pdf