Code Stars,  Python Math

Loops

Making a Turtle

You have learned how to import the turtle module

import turtle

And you have learned how to create a turtle by defining its name.

ty = turtle.Turtle()

You can even make your turtle move forward and turn.

ty.fd(100)

ty.rt(90)

There is a way to get the computer to do the same thing over and over again without having to copy and paste a lot of code.

Loops

Loops are lines of code that run in order for a number of time or set condition that you determine. To create a loop that runs five times use the code below:

for x in range (5):

<what ever commands you want repeated>

<to end the loop stop indenting>

Note: the key word above if for, the x is a variable and you can use whatever you like for it, I choose x since it is the number of times through the loop.

Try making your own loop, printing whatever message you like.

Here is one that says go five times before ending with a big GOOOOOOOO!

Again, the key word is ‘for’.

‘Loop’ could be just about anything.

The (5) says how many times.

Have your turtle draw something using a loop, it doesn’t have to be any specific shape, just make sure you are using a loop.

Leave a Reply

Your email address will not be published. Required fields are marked *