3. Else

else adds a second block to an if. It runs when the if condition is False:

  • else: has no condition and no parentheses. The colon is mandatory.
  • It must sit at the same indentation as its if.
  • Exactly one of the two blocks runs: the if block when the condition is True, the else block when it is False.
  • else cannot exist without an if.
#01

Add a fallback branch

Create a variable age with the value 15. If age >= 18, print Adult; otherwise, print Minor.

The condition is False, so the else block runs.

Print exactly:

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

#02

Decide with input

Read an age with input("Age: ") and convert it with int(). If the age is 18 or more, print Adult; otherwise, print Minor.

The simulated user types 16.

Print exactly:

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

#03

Pass or fail

Create a variable score with the value 42. If score >= 50, print Pass; otherwise, print Fail.

Print exactly:

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

#04

Check a boolean variable

Create a variable logged_in with the value False. If logged_in is True, print Welcome back; otherwise, print Please log in.

Use the variable directly as the condition: it already holds a boolean.

Print exactly:

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