×
  1. Introduction to Python Turtle Graphics.
  2. Setting up your turtle.
  3. Let's get familiar with turtle.
  4. Now is the for loop.
  5. Let's go to the while loop.
  6. If-else conditional statement.
  7. Multiple turtles at once.
  8. Review section.
  9. Advanced knowlegde about turtle.
Image drawn with Turtle.

Python Turtle Graphics

6. if-else conditional statement.

This level 5 will have 2 parts:

  1. Part 1: : You will learn about ‘if-else’ statement/condition in this first part. There will be a simple task in this part.
  2. Part 2: In this part you will have further practice for the for loop and while loop.

Let's take a look at how to use condition in the video

Reminder:: Through the first 3 level of the turtle program, students should have learnt the turtle’s default direction, some common commands to change direction of the turtle, the "for loop" and the "while loop". Here are what you have learnt so far:

Purpose Command
Import Python turtle graphic. import turtle
Create a name for your turtle. name = turtle.Turtle()
Choosing a shape.
if you skip the command choosing a shape for your turtle then your turtle shape will automatically be an arrow.
name.shape(‘shape’)
You have to replace ‘shape’ by one of these shapes: arrow, circle, square, triangle or turtle.
Choosing a color
if you skip the command choosing a color for your turtle then your turtle color will automatically be black.
name.color(‘color’)
You have to replace ‘color’ by the color that you wish to.
Setting a default location to (0,0) name.goto(0,0)
Setting a background
If you don’t want to have background image for your turtle, please skip this part.
Call the screen function. screen=turtle.Screen().
Set background to the picture that you like. screen.bgpic(“picture_file”)
Click to the image icon and look at 3 images that it contains. Replace ‘picture_file’ with the image file that you like.
Movement commands
Make the turtle go forward in the current direction. turtle.forward(steps)
Turns the turtle to the left direction in number degrees. turtle.left(degrees)
Turns the turtle to the right direction in number degrees. turtle.right(degrees)
Pointing a variable to an object. variable = object
The object can be in any type: integer, decimal, list, etc.
Setting turtle pen to a color turtle. pencolor('color')
Replace ‘color’ by a color that you want.
The general while loop is:
n = a
while n < c:
    code chunk
    n = n + b
The general for loop is:
for i inrange(n):
     code chunk
  1. Part 1: “if-else” statement and exercise about ‘if-else’ statement.

  2. General "if-else" statement:
         if (condition):
             "1" intended statement block
         else:
             "2" intended statement block

  3. Part 2:

  4. This is a bus.
    NOW PLEASE DRAW YOUR BUS !
    # This solution will draw out a bus that is the same as the provided picture with length is 90, which is x.

    pi = 3.14
    x = 90
    width = 3*x
    r = x//pi
    fw = x/10
    width = x/3

    my_turtle = turtle.Turtle()
    my_turtle.penup()
    my_turtle.goto(-100,0)
    my_turtle.pendown()
    my_turtle.pensize(5)
    my_turtle.shape('turtle')
    # draw the body
    my_turtle.color('red')
    for i in range(2):
     my_turtle.forward(x*3)
     my_turtle.right(90)
     my_turtle.forward(x)
     my_turtle.right(90)
    # Draw the light
    my_turtle.color('yellow')
    my_turtle.backward(x/2)
    my_turtle.right(90)
    my_turtle.forward(x)
    my_turtle.left(90)
    my_turtle.forward(x/2)
    Draw the black 2 tires
    my_turtle.forward(r)
    my_turtle.right(90)
    my_turtle.color('black')
    my_turtle.circle(r, 180)
    my_turtle.right(90)
    my_turtle.color("red")
    my_turtle.forward(3*x - 6*r)
    my_turtle.right(90)
    my_turtle.color('black')
    my_turtle.circle(r, 180)
    my_turtle.right(90)
    draw windows
    my_turtle.color("red")
    my_turtle.backward(3*x-r)
    my_turtle.left(90)
    my_turtle.forward(x//4)
    my_turtle.right(90)
    my_turtle.color("blue")
    for i in range(6):
     my_turtle.penup()
     my_turtle.forward(fw+width)
     my_turtle.pendown()
     for i in range(4):
      my_turtle.left(90)
      my_turtle.forward(x)

    
    from browser import document
    import turtle
    turtle.set_defaults(
        turtle_canvas_wrapper = document['turtle-div1'])
    
    # DO NOT DELETE ABOVE THIS LINE
    ######## Write your code below
    
    name = turtle.Turtle()
    name.shape("turtle")
    name.color("red")
    
    
    
    # DO NOT DELETE BELOW THIS LINE
    turtle.done() 
    
                



Congratulations! you finish