Interactive Computer Science Tutoring

Learn programming concepts interactively

AHA Schools Logo

Python Conditionals

Learn if/elif/else statements through interactive examples

Learning ModulesInteractive
Choose an interactive example to explore Python conditionals
Traffic Light Simulatorif/elif/else
Learn if/elif/else chains with a traffic light example
Select a traffic light color
Traffic Light Controls
Global Actions
Python Conditionals Code
Live Python code showing if/elif/else examples
# Python Conditionals - Interactive Examples
# Learn if/elif/else statements through hands-on practice
# === TRAFFIC LIGHT SIMULATOR ===
# Example of if/elif/else chain
light_color = "red"
if light_color == "red":
action = "Stop completely"
elif light_color == "yellow":
action = "Prepare to stop"
else:
action = "Go safely"
print(f"Traffic light is {light_color}: {action}")
# === GRADE CALCULATOR ===
# Example of nested if statements
score = 85
if score < 0 or score > 100:
grade = "Invalid"
message = "Score must be between 0 and 100"
else:
if score >= 90:
grade = "A"
message = "Excellent work!"
elif score >= 80:
grade = "B"
message = "Good job!"
elif score >= 70:
grade = "C"
message = "Satisfactory"
elif score >= 60:
grade = "D"
message = "Needs improvement"
else:
grade = "F"
message = "Try again"
print(f"Score: {score} → Grade: {grade} ({message})")
# === INPUT VALIDATOR ===
# Example of boolean conditions and logical operators
user_input = "test@example.com"
validation_type = "email"
if validation_type == "email":
# Simple email validation
is_valid = "@" in user_input and "." in user_input
elif validation_type == "age":
# Age validation
try:
age = int(user_input)
is_valid = 1 <= age <= 149
except ValueError:
is_valid = False
elif validation_type == "password":
# Password strength validation
is_valid = (len(user_input) >= 8 and
any(c.isupper() for c in user_input) and
any(c.isdigit() for c in user_input))
else:
is_valid = False
print(f"Input '{user_input}' is {'valid' if is_valid else 'invalid'}")
# Key Concepts:
# - if/elif/else chains for multiple conditions
# - Nested if statements for complex logic
# - Boolean operators (and, or, not)
# - Comparison operators (==, !=, <, >, <=, >=)
# - Membership operators (in, not in)
History0/5
Recent conditional operations with results

No operations yet

Start by adding or modifying elements