Skip to content

If - Else Statement (if-else)

Use for the scenario that has two decision-making only.

Usage

if meet_condition:
    do action
else:
    do fallback_action
  • The condition must return Boolean value.
  • If the first condition return True than else will handle when condition return False, or vice-versa.

Example

if-else.py

py
age = int(input("Please enter your age: "))

if (age >= 18):
    print("You are eligible to play this game")
else:
    print("You are not allowed to play this game")

Results:

Trial 1: (age = 20)

You are eligible to play this game

Trial 2: (age = 10)

You are not allowed to play this game