Introduction to Programming

Lesson 5 of 7

Conditions

Enable your programs to make smart decisions using if, elif, and else statements.

🟡 Intermediate 90 XP

Learn it

In games, choices matter. If you have a gold key, the castle door opens; otherwise, the door stays locked. This decision-making is called conditional logic.

Using if, elif (short for else if), and else, you can teach your code to check facts and choose the right path to take based on the answers.

Key terms

Condition
A Boolean expression that evaluates to either True or False to steer program flow.
Branching
The ability of a program to follow different execution paths depending on condition outcomes.
Boolean
A fundamental data type having only two possible values: True or False.
Comparison Operator
A symbol used to compare two values, such as == (equality) or > (greater than).

The Decision Pathway

Trace how Python navigates an if-elif-else ladder to evaluate player performance.

  1. 1First Check (IF): Python evaluates if score >= 90. If True, it runs the award block and skips the rest.
  2. 2Second Check (ELIF): If the first test was False, it evaluates elif score >= 50 for the silver reward.
  3. 3Catch-all (ELSE): If none of the above conditions met True, the fallback else block triggers.
  4. 4Resuming Flow: After executing the matched branch, program control continues past the entire structure.

Door Lock Condition Check

pythonhas_key = True
player_level = 5

if has_key and player_level >= 3:
    print("Door unlocked! Enter the dungeon.")
elif has_key and player_level < 3:
    print("You have the key, but your level is too low!")
else:
    print("The door is locked tight. Find the key!")

The program checks both the possession of a key and the required level before deciding which message to output.

Try it

Match each Python comparison operator to its correct meaning.

==
!=
>=
<

Challenge

Create an age-check program with a variable user_age. If age is 12 or under, print Child ticket; elif 13 to 17, print Teen ticket; else print Adult ticket.

Pick whichever way suits you — every mode earns the same bonus XP.

Write at least 40 more characters to submit.

Mark your own work

Guided walkthrough — 0/5 clues revealed

  1. Clue 1 locked — reveal it only if you get stuck.
  2. Clue 2 locked — reveal it only if you get stuck.
  3. Clue 3 locked — reveal it only if you get stuck.
  4. Clue 4 locked — reveal it only if you get stuck.
  5. Clue 5 locked — reveal it only if you get stuck.

Each clue costs 5 XP (never below 23 XP). You'd earn 45 XP right now.

Extension: Add a student_card Boolean variable and give a discount if student_card is True.

Quiz time

Question 1 of 4Score 0

Which keyword begins a conditional check in Python?