Lecture 18 — If Else Assignment
Assignment: If…Else Conditions⌗
In this assignment, you will use if...else statements to create two interactive drawings. Press a key to change each drawing.
Quick Recall⌗
If Statement⌗
An if statement runs a block of code only when a condition is True.
if condition:
# Runs when condition is True
If…Else Statement⌗
An if...else statement chooses between two blocks of code.
if condition:
# Runs when condition is True
else:
# Runs when condition is False
For example, key == "O" checks whether the O key was pressed.
Starter Template⌗
Use this template for both parts of the assignment.
from animation import run_sketch
def draw(p, key):
# Draw the sketch here
run_sketch(draw, bgcolor="lightblue")
Part 1 - Open and Close the Door⌗
Create a door that can open and close when you press a key.

Instructions:
- Press O to open the door.
- Press any other key to close the door.
- Use an
if...elsestatement to draw the door in the correct position. - Keep the door frame and floor visible in both states.
Part 2 - Open and Close the Book⌗
Create a book that can open and close when you press a key.

Instructions:
- Press O to open the book.
- Press any other key to close the book.
- Use an
if...elsestatement to draw the open or closed book. - Make sure the book changes its appearance based on the key pressed.
Submission⌗
Submit both Python files to Google Classroom:
- Part 1 - Door
- Part 2 - Book
Make sure both programs run correctly before submitting.