+120 XP
Next MissionLesson 3: Making Decisions with If and Else
Real life is full of choices: IF it is raining, you take an umbrella, ELSE you wear sunglasses. In Python, we use if, elif, and else statements to give our programs a brain so they can make smart decisions.
Key Concepts to Remember:
- •if Statement: Checks if a condition is True. If it is True, it runs the indented lines below it.
- •elif & else: elif (short for else if) tests another condition if the first was False. else is the fallback if nothing matched.
- •Indentation (4 Spaces): Whenever you put a colon (:) at the end of an if line, the next lines MUST be indented by pressing Tab or 4 spaces.
- •Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than), >=, <=.
💻Branching Score Check (IDE Syntax Colors)
score = int(input("Enter your score: "))
if score >= 100:
print("🏆 Golden Trophy Unlocked!")
elif score >= 50:
print("🥈 Silver Medal Won!")
else:
print("Keep playing! You need 50 points to win a medal.")
💡 Python compares the score against the numbers and runs only the matching branch.
💡 Pro Tip: Do not confuse = (assigns a value to a variable) with == (detective eyes asking "are these two equal?").