5If Statements

An if statement lets your program make decisions based on conditions. It checks whether something is True or False. We can also tell the program to do xyz based on the input from the user.

Example: Imagine we ask the user for their age, and based on their age we can decide what the output or message will be displayed to them.

Code Example:

age = int(input("Enter your age: "))

if age >= 20:
    print("You are allowed to drive a car!")
elif age >= 70:
    print("You are a senior!")
else:
    print("Please try later!")
circle-info

This program asks the user for an input, based on their age we decide: If older or equal to 20, they are allowed to drive a car If older or equal to 70, they are classified as a senior. If ANYTHING else, for example, they are 15 years old, they get a message "Please try later"

Last updated