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

(vip2019) #1
Turtle Graphics: Drawing with Python 27

What You Learned


In this chapter, we drew impressive, colorful shapes in Python using
the Turtle library of tools. We brought this library into our program
by using the import command, and you learned that reusing code
in this way is one of the most powerful things about programming.
Once we’ve written something useful, or borrowed code that some-
one else has been kind enough to share, we not only save time but
can also use that imported code to do neat new things.
You’ve also been introduced to variables like x and sides in
our programs. These variables store, or remember, a number or
value so that you can use it multiple times in a program and even
change the value as you go. In the next chapter, you’ll learn more
about the power of variables and how Python can even help you do
your math homework!
At this point, you should be able to do the following:
• Draw simple graphics with the Turtle library.
• Use variables to store simple number values and strings.
• Change, save, and run programs in IDLE.

p ogramming r challenges
Try these challenges to practice what you’ve learned in this
chapter. (If you get stuck, go to http://www.nostarch.com/
teachkids/ for sample answers.)

#1: Changing the Number of Sides


We used a variable, sides, in the ColorSpiral.py program
on page 25, but we didn’t vary it much or change its value
except for editing, saving, and running the program again.
Try changing the value of sides to another number, say 5.
Save and run the program to see how this affects your
drawing. Now try 4, 3, 2, and even 1! Now, add two or more
colors, in quotes, separated by commas, to the list of colors
in the sixth line of the program. Increase the value of sides
to use this new number of colors—try 8 or 10 or more!

continued
Free download pdf