Lecture 16 - Keyboard Input and If Statements
Lecture 16 — Keyboard Input and If Statements⌗
Instructor: Laksh Budhrani
Objectives⌗
- Understand how
key == "A"checks whether the A key was pressed. - Use
ifstatements to change the appearance of objects based on keyboard input.
Quick Recall — If Statements⌗
An if statement checks whether a condition is True or False.
- If the condition is
True, Python executes the indented code. - If the condition is
False, Python skips the indented code.
Syntax⌗
if condition:
# Code runs when the condition is True
Objective 1 & 2 — Checking Keyboard Input⌗
In our animations, we can use the key variable to check which key was pressed.
For example:
if key == "A":
print("A was pressed!")
The == operator checks whether two values are equal.
- If the A key was pressed,
key == "A"evaluates toTrue. - If another key was pressed, it evaluates to
False.
Therefore, the print statement runs only when the A key is pressed.
Update animation.py⌗
Before starting the activities, replace the code in your existing animation.py file with the code below.
This updated version passes the most recently pressed key to your draw() function through the key parameter.
import turtle
def run_sketch(draw_function, width=600, height=400,
fps=30, bgcolor="skyblue"):
screen = turtle.Screen()
screen.setup(width, height)
screen.bgcolor(bgcolor)
screen.tracer(0)
p = turtle.Turtle()
p.speed(0)
p.pensize(2)
last_key = ""
def handle_key(key):
nonlocal last_key
last_key = key.upper()
screen.listen()
for key in "abcdefghijklmnopqrstuvwxyz":
screen.onkeypress(lambda k=key: handle_key(k), key)
for key in "0123456789":
screen.onkeypress(lambda k=key: handle_key(k), key)
screen.onkeypress(lambda: handle_key("SPACE"), "space")
def frame_tick():
p.clear()
p.penup()
p.goto(0, 0)
p.setheading(0)
p.pendown()
draw_function(p, last_key)
screen.update()
screen.ontimer(frame_tick, int(1000 / fps))
screen.ontimer(frame_tick, 100)
screen.mainloop()
Important: You do not need to modify animation.py during the activities. You will write your code in a separate Python file.
Starter Template⌗
Use the following template when starting each activity. Create a separate Python file for each activity and copy this template into it.
from animation import run_sketch
# Part 1 - setup. runs one time
# Part 2 - draw. repeats 60 fps
def draw(p, key):
pass
# Draw the sketch here
# don't write anything below this line
run_sketch(draw, bgcolor="lightblue")
Guided Activity — Light Bulb⌗
Create a new Python file named light_bulb.py using the starter template above.
Create a light bulb that changes color when a key is pressed.
Reference Images⌗
Add the screenshots showing the bulb before and after it is turned on.

Requirements⌗
- Initially, the bulb should be gray.
- Press O to turn on the bulb.
- When turned on, the bulb should change to yellow.
- Use an
ifstatement to change the bulb’s color.
Independent Activity — Magic Wand⌗
Create a new Python file named magic_wand.py using the starter template above.
Create a magic wand with a star at its tip.
Reference Images⌗
Add the screenshots showing the star before and after the magic is activated.

Requirements⌗
- Draw a brown magic wand.
- Initially, the star should be gray.
- Press S to activate the magic.
- When activated, the star should change to gold.
- Use an
ifstatement to change the star’s color.
Bonus — Rocket Launch⌗
Create a new Python file named rocket.py using the starter template above.
Create a rocket that produces flames when activated.
Reference Images⌗
Add the screenshots showing the rocket before and after it is activated.

Requirements⌗
- Draw a rocket with a window and fins.
- Initially, the rocket should have no flames.
- Press R to activate the rocket.
- When activated, orange and yellow flames should appear below the rocket.
- Use an
ifstatement to display the flames.
Submission Instructions⌗
Submit your Python files for both the Guided and Independent Activities.
The bonus is optional.