Turtle Graphics with Conditionals

Conditionals

An if conditional statement can be True or False. When the if statement executes, the condition is tested and if it is true, the indented block of code is executed.

if <conditional statement>:

<do something>

else:

<do something>

If you have a pattern where every other statement alternates, you can use use the values of the stepper variable to determine if the value of the variable is odd or even.

Star

star3.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('red')
for x in range(20):
        turtle.forward(100)
        if x % 2 == 0:
            turtle.left(175)
        else:
            turtle.left(225)

ZigZag

zigzag2.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('red')
for x in range(10):
    if x % 2 == 0:
        turtle.forward(25)
        turtle.right(45)
    else:
        turtle.forward(25)
        turtle.right(45)

Last updated