Making Decisions with if / elif / else

About 15 minutes

Why this matters

Useful programs do different things in different situations: show an error when a password is wrong, give a discount to members, say “good morning” or “good evening”. In Python, you make these choices with if.

The idea

An if statement runs some code only when a condition is true:

temperature = 30

if temperature > 20:
    print("Warm day")

Three details matter here:

  1. The condition (temperature > 20) is followed by a colon :.
  2. The code that belongs to the if is indented (four spaces is the convention). Indentation is how Python knows which lines are inside the if.
  3. If the condition is false, the indented code is skipped.

The condition is any comparison that gives True or False, like the ones you met in the last lesson: >, <, >=, <=, == and !=. Remember the double == for comparing; a single = is assignment.

Add elif (“else if”) for more cases and else for everything left over. Python checks them top to bottom and runs only the first one that is true:

score = 72

if score >= 90:
    print("A")
elif score >= 70:
    print("B")
else:
    print("Below B")

Try it

Change score in the example above and run it a few times. Try 95, 70, 69 and 0. Can you predict the result before pressing Run?

Your turn

The starter code asks the user for a temperature and stores it in temperature. (You met input() and int() two lessons ago: it reads what the user types and turns it into a number.)

Add an if / elif / else that prints exactly one word:

Temperature Print
25 or higher Hot
15 or higher, but below 25 Nice
anything lower Cold

In the Input box under the editor, type a number and press Run to try your program. Check will try several temperatures for you.

Recap

  • if condition: runs the indented block only when the condition is true.
  • elif and else add more branches; only the first matching branch runs.
  • Use == to compare and = to assign.

Common mistakes

  • Forgetting the colon after if, elif or else gives a SyntaxError.
  • Forgetting to indent the block gives an IndentationError.
  • Using = instead of == in a condition gives a SyntaxError.
  • Ordering conditions badly: checking >= 15 before >= 25 would make 30 print Nice. Start with the most specific case.

Practice

Tip: press Ctrl/⌘ + Enter to run. Press Esc, then Tab, to move out of the editor.

Input for input()

Loading Python…

Output

    Need help?