Objectives⌗

  1. Understand how an if...else statement allows a program to choose between two actions.
  2. Understand when Python executes the if block and when it executes the else block.
  3. Use if...else statements to change the appearance of objects based on keyboard input.

Quick Recall — If Statements⌗

An if statement executes its code only when the condition is True.

if condition:
    # Code runs when the condition is True

What happens when the condition is False?

Python simply skips the indented code.

What if we want the program to do something else when the condition is False?

We can use an else statement!


Objective 1 — Understanding If…Else⌗

An if...else statement allows a program to choose between two actions.

  • if: Runs when the condition is True.
  • else: Runs when the condition is False.

Syntax⌗

if condition:
    # Code runs when the condition is True
else:
    # Code runs when the condition is False

Objective 2 — How If…Else Works⌗

Let’s look at an example.

is_raining = True

if is_raining:
    print("Take an umbrella!")
else:
    print("No umbrella needed!")
  • Since is_raining is True, Python executes the if block.
  • If is_raining were False, Python would execute the else block instead.

Remember: Only one of the two blocks will execute.


Objective 3 — Using If…Else with Keyboard Input⌗

We can also use if...else with keyboard input to change the appearance of objects in our animations.

For example:

if key == "S":
    color = "red"
else:
    color = "green"
  • If the S key was pressed, the color becomes red.
  • Otherwise, the color becomes green.

This allows us to create drawings that can switch between two different appearances.


Starter Template⌗

Create a new Python file for each activity and use the following template.

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 — Happy and Sad Face⌗

Create a new Python file named smiley_face.py.

Create a smiley face that can change its expression.

Reference Images⌗

Add the screenshots showing the happy and sad expressions.

Happy and sad face

Requirements⌗

  • Initially, display a happy face.
  • Press S to show a sad face.
  • Press H to show a happy face.
  • Use an if...else statement to draw the appropriate mouth.

Independent Activity — Flower⌗

Create a new Python file named flower.py.

Create a flower that can bloom and close.

Reference Images⌗

Add the screenshots showing the closed and open flower.

Closed and open flower

Requirements⌗

  • Draw a flower with a stem and petals.
  • Initially, the flower should be closed.
  • Press B to bloom the flower.
  • Press C to close the flower.
  • Use an if...else statement to display the open or closed flower.

Bonus — Candle⌗

Create a new Python file named candle.py.

Create a candle that can be turned on and off.

Reference Images⌗

Add the screenshots showing the unlit and lit candle.

Unlit and lit candle

Requirements⌗

  • Draw a candle with a wick.
  • Initially, the candle should be unlit.
  • Press L to light the candle.
  • Press O to turn off the candle.
  • Use an if...else statement to display the flame or the unlit wick.

Submission Instructions⌗

Submit your Python files for all 3 parts.