Skip to main content

If Statements

In the Python programming language, if statements allow you to write code that only runs sometimes.

Dependencies

Students need to have these skills before learning this skill:

Goal

When you teach this skill, your goal is to make it so that students can write code that looks like this:

name = input("What is your name? ")
if name == "Perry the Platypus":
print("Curse you Perry the Platypus!")

Activities

Here are some activities that you can do in order to teach this skill.

Writing on the board

  1. Write this code on the board:

    favorite_color = input("What is your favorite color? ")
    if favorite_color == "Orange":
    print("That’s my favorite color too!")
  2. Ask students what they think that code is going to do.

  3. After calling on a few students, reveal the correct answer.

  4. Ask all students to write similar code on their computers. Their code should ask a different question an react to a different answer.

  5. Have students run their code in order to make sure that it works.