Teach Your Kids To Code: A Parent-friendly Guide to Python Programming
Random Fun and Games: Go Ahead, Take a Chance! 135 Our program begins with the turtle and random modules imported as usual, but ...
136 Chapter 6 Figure 6-7: The mirrored/repeated effect in Kaleidoscope.py. What You Learned Before this chapter, we had no way o ...
Random Fun and Games: Go Ahead, Take a Chance! 137 We combined our ability to choose an item from a list at random using random. ...
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 ...
Random Fun and Games: Go Ahead, Take a Chance! 139 thickness. Add the line t.width(thick) in the right place to change the thick ...
...
7 Functions: there’s a name for that We’ve made use of a number of functions so far— everything from print() to input() to turtl ...
142 Chapter 7 Functions are helpful because they give us the ability to orga- nize pieces of reusable code, then refer to those ...
Functions: There’s a Name for That 143 Say we want to use that random spiral code in another program, like a game or a screensav ...
144 Chapter 7 on the screen, and then move the pen there and draw the spiral. Here’s the code to complete our random_spiral() fu ...
Functions: There’s a Name for That 145 Here’s the whole program together; type this into IDLE and save it as RandomSpiralsFuncti ...
146 Chapter 7 Parameters: Feeding Your Function When creating a function, we can define parameters for that function. Parameters ...
Functions: There’s a Name for That 147 sitting on the (x, y) location, so picture moving this smiley face template around by pla ...
148 Chapter 7 To fill the circle with yellow, we add four lines of code around our t.circle(50) command. First, we set the pen c ...
Functions: There’s a Name for That 149 the faces will begin at (0, 0). The command t.setpos(x-15, y+60) will make sure that wher ...
150 Chapter 7 these pairs of points are mirror images of one another across the y-axis; this makes our smiley face nice and even ...
Functions: There’s a Name for That 151 The final piece will be our for loop to generate 50 random locations for the smiley faces ...
152 Chapter 7 # Right eye t.setpos(x+15, y+60) t.begin_fill() t.circle(10) t.end_fill() # Mouth t.setpos(x-25, y+40) t.pencolor( ...
Functions: There’s a Name for That 153 Figure 7-2: The RandomSmileys.py program produces a happy result. r turn: e it’s What You ...
154 Chapter 7 To convert a length in inches to its equivalent in centimeters, we multiply the number of inches by 2.54—the appro ...
«
4
5
6
7
8
9
10
11
12
13
»
Free download pdf