Skip to content

Exercise Solutions

These are a solution to each exercise. There is usually more than one way to solve a problem, so any program that produces the required result solves it.

Lesson 1: Thonny Introduction

Exercise 1

print("Hello Alex")

Exercise 2

1
2
3
print("My name is Alex")
print("My favourite food is pizza")
print("My favourite game is Minecraft")

Exercise 3

1
2
3
print("Hello")
print("How are you?")
print("Goodbye")

Lesson 2: Introducing Turtle

Exercise 1

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

my_ttl.forward(100)
my_ttl.left(90)
my_ttl.forward(100)
my_ttl.left(90)
my_ttl.forward(100)
my_ttl.left(90)
my_ttl.forward(100)
my_ttl.left(90)

Exercise 2

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

my_ttl.forward(100)
my_ttl.left(120)
my_ttl.forward(100)
my_ttl.left(120)
my_ttl.forward(100)
my_ttl.left(120)

Exercise 3

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

my_ttl.forward(100)
my_ttl.left(60)
my_ttl.forward(100)
my_ttl.left(60)
my_ttl.forward(100)
my_ttl.left(60)
my_ttl.forward(100)
my_ttl.left(60)
my_ttl.forward(100)
my_ttl.left(60)
my_ttl.forward(100)
my_ttl.left(60)

Lesson 3: Iteration

Exercise 1

1
2
3
4
friends = ["Mia", "Noah", "Ava", "Leo"]

for friend in friends:
    print("Hello", friend)

Exercise 2

1
2
3
4
5
6
7
names = ["Hunter", "Jordi", "Adam", "Jesse", "Bryce", "Ben"]

for name in names:
    print("Good morning", name)
    print("Please get your laptop out")

print("Let's start the lesson")

Exercise 3

Come in and sit down is printed six times, once after each How are you?. Indenting the line makes it part of the for loop's code block, so it repeats every time the loop repeats instead of running once after the loop has finished.

Lesson 4: Range

Exercise 1

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

for side in range(1, 5):
    my_ttl.forward(100)
    my_ttl.left(90)

Exercise 2

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

for side in range(1, 4):
    my_ttl.forward(100)
    my_ttl.left(120)

Exercise 3

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

for side in range(1, 7):
    my_ttl.forward(100)
    my_ttl.left(60)

Exercise 4

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

for side in range(1, 361):
    my_ttl.forward(1)
    my_ttl.left(1)

Exercise 5

This is one idea: a square spiral that grows as the loop repeats, because the turtle moves forward by the loop's current number each time.

import turtle

window = turtle.Screen()
window.setup(500, 500)

my_ttl = turtle.Turtle()

for number in range(1, 201):
    my_ttl.forward(number)
    my_ttl.left(91)

Lesson 5: Variables

Exercise 1

import turtle

screen = 500
sides = 4
length = 100
CIRCLE_DEG = 360

window = turtle.Screen()
window.setup(screen, screen)
my_ttl = turtle.Turtle()

for index in range(sides):
    my_ttl.forward(length)
    my_ttl.left(CIRCLE_DEG / sides)

Exercise 2

import turtle

screen = 500
sides = 360
length = 1
CIRCLE_DEG = 360

window = turtle.Screen()
window.setup(screen, screen)
my_ttl = turtle.Turtle()

for index in range(sides):
    my_ttl.forward(length)
    my_ttl.left(CIRCLE_DEG / sides)

Exercise 3

import turtle

screen = 500
sides = 5
length = 100
CIRCLE_DEG = 360

window = turtle.Screen()
window.setup(screen, screen)
my_ttl = turtle.Turtle()

for index in range(sides):
    my_ttl.forward(length)
    my_ttl.left(CIRCLE_DEG / sides)

Lesson 6: Coordinates

This is the house from the start of Lesson 7.

Exercise 1

import turtle

# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)

# create turtle instance
my_ttl = turtle.Turtle()
my_ttl.shape("arrow")

# move pen
my_ttl.penup()
my_ttl.goto(-100, 0)
my_ttl.pendown()

# draw square
for index in range(4):
    my_ttl.forward(200)
    my_ttl.right(90)

# draw triangle
for index in range(3):
    my_ttl.forward(200)
    my_ttl.left(120)

# move pen
my_ttl.penup()
my_ttl.goto(-25, -200)
my_ttl.pendown()

# draw rectangle
for index in range(2):
    my_ttl.forward(50)
    my_ttl.left(90)
    my_ttl.forward(100)
    my_ttl.left(90)

# move pen
my_ttl.penup()
my_ttl.goto(-80, -100)
my_ttl.pendown()

# draw square
for index in range(4):
    my_ttl.forward(35)
    my_ttl.right(90)

# move pen
my_ttl.penup()
my_ttl.goto(45, -100)
my_ttl.pendown()

# draw square
for index in range(4):
    my_ttl.forward(35)
    my_ttl.right(90)

# move pen
my_ttl.penup()
my_ttl.goto(15, -150)
my_ttl.pendown()

# draw circle
my_ttl.circle(5)
my_ttl.hideturtle()

Lesson 7: Functions

Exercise 1

import turtle


def move_pen(x, y):
    my_ttl.penup()
    my_ttl.goto(x, y)
    my_ttl.pendown()


def draw_circle(radius, colour):
    my_ttl.color("black", colour)
    my_ttl.begin_fill()
    my_ttl.circle(radius)
    my_ttl.end_fill()


def draw_rect(width, height, colour):
    my_ttl.color("black", colour)
    my_ttl.begin_fill()
    for index in range(2):
        my_ttl.forward(width)
        my_ttl.right(90)
        my_ttl.forward(height)
        my_ttl.right(90)
    my_ttl.end_fill()


# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)

# create turtle instance
my_ttl = turtle.Turtle()
my_ttl.shape("arrow")

# draw head
move_pen(0, -200)
draw_circle(200, "yellow")

# draw eyes
move_pen(-75, 0)
draw_circle(50, "black")
move_pen(75, 0)
draw_circle(50, "black")

# draw mouth
move_pen(-100, -75)
draw_rect(200, 25, "black")
my_ttl.hideturtle()

Exercise 2

import turtle


def move_pen(x, y):
    my_ttl.penup()
    my_ttl.goto(x, y)
    my_ttl.pendown()


def draw_circle(radius, colour):
    my_ttl.color(colour, colour)
    my_ttl.begin_fill()
    my_ttl.circle(radius)
    my_ttl.end_fill()


def draw_rect(width, height, colour):
    my_ttl.color(colour, colour)
    my_ttl.begin_fill()
    for index in range(2):
        my_ttl.forward(width)
        my_ttl.right(90)
        my_ttl.forward(height)
        my_ttl.right(90)
    my_ttl.end_fill()


# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)

# create turtle instance
my_ttl = turtle.Turtle()
my_ttl.shape("arrow")

# draw body
move_pen(-200, 0)
draw_rect(400, 100, "red")
move_pen(-100, 100)
draw_rect(200, 100, "red")

# draw glass
move_pen(-90, 90)
draw_rect(80, 80, "cyan")
move_pen(10, 90)
draw_rect(80, 80, "cyan")

# draw wheels
move_pen(-100, -150)
draw_circle(50, "black")
move_pen(100, -150)
draw_circle(50, "black")

# draw hubs
move_pen(-100, -125)
draw_circle(25, "silver")
move_pen(100, -125)
draw_circle(25, "silver")

# draw headlights
move_pen(200, -25)
draw_rect(10, 25, "yellow")

# draw bumpers
move_pen(175, -75)
draw_rect(40, 10, "silver")
move_pen(-215, -75)
draw_rect(40, 10, "silver")

my_ttl.hideturtle()

Lesson 8: User Input

Exercise 1

1
2
3
4
number = int(input("What number should I count up to?> "))

for count in range(1, number + 1):
    print(count)

Lesson 9: Branching

Exercise 1

1
2
3
4
5
6
name = input("What is your name?> ")

if name == "Amy":
    print("Welcome Amy, please come in")
else:
    print("Sorry, you can't come in. Please go away")

Exercise 2

friend = "Bruce"

name = input("What is your name?> ")

if name == "Amy":
    print("Welcome Amy, please come in")
elif name == friend:
    print("Welcome, friend of Amy, please come in")
else:
    print("Sorry, you can't come in. Please go away")

Exercise 3

import turtle


def draw_poly(length, sides, color):
    my_ttl.color("black", color)
    my_ttl.begin_fill()
    for index in range(sides):
        my_ttl.forward(length)
        my_ttl.right(360 / sides)
    my_ttl.end_fill()


def get_number(prompt):
    num = input(prompt)
    if num.lstrip("-").isdigit():
        return int(num)
    else:
        print("Invalid input")
        quit()


def get_color():
    color = input("Fill colour (red, blue, green)?> ").lower()
    if color == "red":
        return color
    elif color == "blue":
        return color
    elif color == "green":
        return color
    else:
        print("Invalid input")
        quit()


def move_pen():
    x = get_number("x position?> ")
    y = get_number("y position?> ")
    my_ttl.penup()
    my_ttl.goto(x, y)
    my_ttl.pendown()


# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)

# create turtle instance
my_ttl = turtle.Turtle()
my_ttl.shape("turtle")

# get user input
sides = get_number("How many sides?> ")
length = get_number("How long are the sides?> ")
fill = get_color()

move_pen()
draw_poly(length, sides, fill)

Lesson 10: While Loops

Exercise 1

import turtle


def draw_poly(length, sides, color):
    my_ttl.color("black", color)
    my_ttl.begin_fill()
    for index in range(sides):
        my_ttl.forward(length)
        my_ttl.right(360 / sides)
    my_ttl.end_fill()


def get_number(prompt):
    while True:
        num = input(prompt)
        if num.lstrip("-").isdigit():
            return int(num)
        else:
            print("Invalid input")


def get_color():
    while True:
        color = input("Fill colour (red, blue, green)?> ").lower()
        if color == "red":
            return color
        elif color == "blue":
            return color
        elif color == "green":
            return color
        else:
            print("Invalid input")


def move_pen():
    x = get_number("x position?> ")
    y = get_number("y position?> ")
    my_ttl.penup()
    my_ttl.goto(x, y)
    my_ttl.pendown()


# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)

# create turtle instance
my_ttl = turtle.Turtle()
my_ttl.shape("turtle")

# get user input
sides = get_number("How many sides?> ")
length = get_number("How long are the sides?> ")
fill = get_color()

move_pen()
draw_poly(length, sides, fill)

Lesson 11: Boolean Logic

Exercise 1

1
2
3
4
5
print(1 != 2)
print(300 >= 250)
print(10 > 5 and 5 > 4)
print(6 > 5 or 1 == 2)
print(not False)

Exercise 2

1
2
3
score = int(input("What was your score?> "))

print(score >= 50 and score <= 100)

Exercise 3

1
2
3
day = input("What day is it?> ").lower()

print(day == "saturday" or day == "sunday")

Lesson 12: Mouse Input

Exercise 1

import turtle


def set_scene():
    turtle.setup(800, 600)
    turtle.onscreenclick(draw_dot)
    my_ttl.speed(0)
    for index in range(2):
        my_ttl.forward(400)
        my_ttl.back(400)
        my_ttl.right(90)
        my_ttl.forward(300)
        my_ttl.back(300)
        my_ttl.right(90)
    my_ttl.penup()


def draw_dot(x, y):
    print(x, y)
    color = "orange"
    if x > 0 and y > 0:
        color = "red"
    size = 10
    my_ttl.goto(x, y)
    my_ttl.dot(size, color)


my_ttl = turtle.Turtle()
set_scene()
my_ttl.hideturtle()
turtle.done()

Exercise 2

import turtle


def set_scene():
    turtle.setup(800, 600)
    turtle.onscreenclick(draw_dot)
    my_ttl.speed(0)
    for index in range(2):
        my_ttl.forward(400)
        my_ttl.back(400)
        my_ttl.right(90)
        my_ttl.forward(300)
        my_ttl.back(300)
        my_ttl.right(90)
    my_ttl.penup()


def draw_dot(x, y):
    print(x, y)
    color = "orange"
    if x > 0 and y > 0:
        color = "red"
    else:
        color = "green"
    size = 10
    my_ttl.goto(x, y)
    my_ttl.dot(size, color)


my_ttl = turtle.Turtle()
set_scene()
my_ttl.hideturtle()
turtle.done()

Exercise 3

import turtle


def set_scene():
    turtle.setup(800, 600)
    turtle.onscreenclick(draw_dot)
    my_ttl.speed(0)
    for index in range(2):
        my_ttl.forward(400)
        my_ttl.back(400)
        my_ttl.right(90)
        my_ttl.forward(300)
        my_ttl.back(300)
        my_ttl.right(90)
    my_ttl.penup()


def draw_dot(x, y):
    print(x, y)
    color = "orange"
    if x > 0 and y > 0:
        color = "red"
    elif x < 0 and y > 0:
        color = "blue"
    elif x < 0 and y < 0:
        color = "yellow"
    else:
        color = "green"
    size = 10
    my_ttl.goto(x, y)
    my_ttl.dot(size, color)


my_ttl = turtle.Turtle()
set_scene()
my_ttl.hideturtle()
turtle.done()