LESSON 09

Making Decisions

Until now, your program ran every line, every time. Now it chooses.

USES: variables, comparisons and True/False (Lesson 8), input( ) and int( ) (Lessons 6 and 7), .lower( ) (Lesson 4).

01

The bouncer gets a job

In Lesson 8 your bouncer learned to ask 'are you 18 or older?' and answer True or False. But the bouncer never did anything with the answer. This lesson fixes that. Read it like English: if age is at least 18, then print "Welcome in".

INPUTage = 20 if age >= 18: print("Welcome in")
OUTPUTWelcome in
TIPIndentation is how Python knows which lines belong together. Many languages use brackets for this. Python uses spaces, which is why its code tends to look tidy.
METAPHORThe if is the bouncer with a clipboard: he checks one rule, and if the answer is yes, the indented lines run. Where it stops working: a real bouncer can use judgment. Python can only follow the rule exactly as written.

BLUEPRINT ยท TAP OR HOVER EACH PIECE

Blueprint 1 of 1: the anatomy of an if. Tap each piece, including the empty gap at the start of the second line.

02

When the answer is False

What if the person is too young? The condition is False, so the indented line is skipped. But print("Done") has no indentation, so it does not belong to the if. It runs every time.

INPUTage = 15 if age >= 18: print("Welcome in") print("Done")
OUTPUTDone
TIPThis is the key idea of the lesson: indented lines run only when the condition is True. Lines without indentation run no matter what.

03

else, the other path

Often you want one thing when the answer is True and something else when it is False. That is else. Exactly one of the two paths runs, never both and never neither.

INPUTage = 15 if age >= 18: print("Welcome in") else: print("Sorry, not yet")
OUTPUTSorry, not yet
TIPelse has no condition, because it catches everything that is left. It goes back to the same indentation as its if, and it also ends in a colon.
METAPHORA fork in the road: you take one way, and the roads join up again below.

04

elif, more than two paths

Sometimes two paths are not enough. elif is short for 'else if'. Python checks the conditions from the top, one at a time. The first one that is True runs, and then Python skips the rest.

INPUTscore = 72 if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") else: print("F")
OUTPUTC
TIPAs many elif branches as you like, and the final else is optional.
METAPHORA price table at the post office: under 1 kg costs 5, under 5 kg costs 10, anything else 20. You read from the top and stop at the first line that fits. Where it stops working: a person can read every line before choosing. Python stops at the first match and never looks at the rest.

05

Order matters, because the first match wins

What if the easy condition comes first? A score of 95 should be an A, but 95 >= 70 is True, so Python stops at the first branch and prints C. Put the strictest condition first.

INPUTscore = 95 if score >= 70: print("C") elif score >= 90: print("A")
OUTPUTC

06

Decisions with input

Now combine it with everything from earlier lessons. Notice the int( ): this is the trap from Lessons 7 and 8. input( ) gives text, and text is never equal to or greater than a number. Convert first, then compare.

INPUTage = int(input("How old are you? ")) if age >= 18: print("Welcome in") else: print("Sorry, not yet")
OUTPUTWelcome in
TIPRun it again in your head with 12: you get 'Sorry, not yet'. Same code, different path. That is the whole point.

07

Decisions with text

Text works too, and it is where .lower( ) earns its place. Without it, "YES" == "yes" is False, because Python is case-sensitive (Lesson 8). Lowercasing the answer first means every spelling of yes works.

INPUTanswer = input("Do you like Python? ") answer = answer.lower() if answer == "yes": print("Great!") else: print("Give it time.")
OUTPUTGreat!

08

Reading error messages

Three mistakes show up constantly, so here they are on purpose. Missing indentation gives an IndentationError: expected an indented block. A missing colon gives SyntaxError: expected ':'. One = instead of two gives a SyntaxError in which Python even guesses you meant ==. None of these mean you are doing badly. Python is pointing at the exact spot: read the message, look at the line it names, fix it.

INPUTage = 20 if age >= 18: print("Welcome in")
OUTPUTIndentationError: expected an indented block after 'if' statement on line 2
TIPKeep indentation consistent: always four spaces, never a mix of tabs and spaces. In the Run box, Tab now inserts four spaces for you, and Enter after a colon indents the next line automatically.

09

Quick quiz: which branch runs?

temp = 30 if temp > 25: print("Hot") elif temp > 15: print("Nice") else: print("Cold") What prints?

10

Quick quiz: one if or two?

x = 5 if x > 3: print("A") if x > 4: print("B") else: print("C") What prints?

11

Quick quiz: which line always runs?

n = 3 if n > 5: print("big") print("end") Which line always runs?

Report a mistake or suggest a lesson