4. Elif

elif adds more conditions to an if chain:

  • elif means "else if". It has its own condition and a colon.
  • Python checks the conditions from top to bottom. An elif is tested only when all the previous conditions were False.
  • The first condition that is True wins: its block runs and the rest of the chain is skipped.
  • else is optional. When present, it runs only when every condition is False.
#01

Grade a score

Create a variable score with the value 85. Use an if/elif/else chain:

  • score >= 90 prints A
  • score >= 80 prints B
  • otherwise, print C

Only the first true condition runs.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Child, teen or adult

Read an age with input("Age: ") and convert it with int(). Use a single if/elif/else chain to print:

  • Child when the age is less than 13
  • Teen when the age is less than 18
  • Adult otherwise

The simulated user types 15.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

The first match wins

Create a variable temperature with the value 25. Print:

  • Warm when temperature >= 25
  • Mild when temperature >= 15
  • Cold otherwise

25 satisfies both of the first two conditions. Only the first match runs.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Match a color

Create a variable color with the value "yellow". Print:

  • Stop when color is "red"
  • Wait when color is "yellow"
  • Go when color is "green"
  • Unknown for anything else

Print exactly:

Create a free account to save your progress and unlock chapter 2.