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

(vip2019) #1
Functions: There’s a Name for That 173

Programming Challenges


Here are three challenges to extend what you learned in
this chapter. For sample answers to these challenges, go to
http://www.nostarch.com/teachkids/.


#1: Mirrored Smileys


Create a mashup of the programs ClickAndSmile.py and
ClickKaleidoscope.py to draw a smiley face in four mirrored
corners of the screen when you click, just like the kaleido-
scope program did with spirals. If you’d like an advanced
challenge, draw two of the smiley faces flipped upside down
so that they really look mirrored across the x-axis.


#2: More Ping-Pong Calculations


Modify the Ping-Pong calculator so it asks the user for a
number of Ping-Pong balls as input. Have it tell the user
both how tall those Ping-Pong balls would be if stacked on
top of one another and how much that number of Ping-Pong
balls would weigh.


#3: A Better Drawing Program


Change the ArrowDraw.py program to allow the user to
turn the turtle in smaller increments—say 45 degrees
(or even 30 or 15)—to give them finer control of the turtle.
Then, add more key options, like allowing the user to press
the greater-than symbol (>) to make the drawing length
longer, the less-than symbol (<) to shorten the drawing
length, the W key to make the pen wider, and the T key to
make the pen thinner. To make it a great drawing program,
add feedback in the form of drawing a string on the screen
to show the pen width, segment length, and turtle’s direc-
tion after every change.
For a finishing touch, add the ability to click to reposi-
tion the turtle. (Hint: create a function that accepts two
parameters (x, y), lifts the turtle’s pen, moves to (x, y), then
puts the pen back down. Then, pass the name of this func-
tion to turtle.onscreenclick() to complete the app.)

Free download pdf