The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

104 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


To turn that into a snowflake-like shape, click the Stop icon in the main Thonny window and
create a loop around your loop by adding the following line as line 3:

for i in range( 10 ):

...and the following at the bottom of your program:

pat.right( 36 )

Your program won’t run as it is, because the existing loop isn’t indented correctly. To fix that,
click on the start of each line in the existing loop – lines 4 through 8 – and press the SPACE
key four times to correct the indentation. Your program should now look like this:

import turtle
pat = turtle.Turtle()
for i in range( 10 ):
for i in range( 2 ):
pat.forward( 100 )
pat.right( 60 )
pat.forward( 100 )
pat.right( 120 )
pat.right( 36 )

Click the Run icon, and watch the turtle: it’ll draw a parallelogram, as before, but when
it’s done it’ll turn 36 degrees and draw another, then another, and so on until there are ten
overlapping parallelograms on the screen – looking a little like a snowflake (Figure 5-10).

5 Figure 5-10: Repeating the shape to make a more complex one
Free download pdf