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

(vip2019) #1

292 Appendix C


Figure C-1: Two colorful spirals created with a three-
line program, thanks to the colorspiral.py module

With the colorspiral module, anytime a programmer wants to
create colorful spirals, all they have to do is import the module and
call colorspiral.cspiral()!

r using the colorspiral Modulee
Let’s reuse the colorspiral module to draw 30 random, colorful
spirals. To do that, we’ll import another module we’ve used before,
random. Type the following eight lines of code into a new file in
IDLE and save the file as SuperSpiral.py.
SuperSpiral.py

import colorspiral
import random
for n in range(30):
sides = random.randint(3,6)
size = random.randint(25,75)
x = random.randint(-300,300)
y = random.randint(-300,300)
colorspiral.cspiral(sides, size, x, y)

This program begins with two import statements: one for the
colorspiral module we created and the other for the random module
we’ve used throughout the book. The for loop will run 30 times.
The loop generates four random values for the number of sides
Free download pdf