Card 5: If / Else

Module 2: Making Decisions

1 The Hook

The IF Statement is like a Club Bouncer.

It checks your ID (Condition).

  • IF you are old enough -> "Come in!"
  • ELSE (otherwise) -> "Go home!"

2 Worked Example

Notice the colon (:) and the indentation.

password = input("Password: ") # Ask for password
if password == "1234": # CHECK: Is it "1234"? Note the COLON :
print("Welcome!") # Indented: Runs only if True
else: # OTHERWISE (if False)
print("Wrong!") # Indented: Runs only if False

PREDICT

age is 15. What prints?

if age >= 18:
  print("Vote")
else:
  print("Wait")

Interactive Editor

> _
BRONZE (MODIFY)

🥉 New High Score

1. Copy the code.
2. Change the Threshold from 50 to 90.
3. Run it to check if it says "Failed" (because 75 is not > 90).

score = 75
if score > 50:
  print("Winner")
> _
SILVER (FIX)

🥈 Missing Colon

The code below is broken. We forgot the Colon (:)!
Fix it so it runs.

> _
GOLD (MAKE)

🥇 Password Protection

1. Ask the user for a pin (stored as text).
2. If pin == "1234", print "Unlock".
3. Else, print "Locked".

> _