Conditional Statements Checkpoint
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Consolidate your conditional statements mastery by tackling classic technical interview coding problems.
Leap year logic
A year is a leap year if it is divisible by 4, except end-of-century years which must be divisible by 400.
year = 2024is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)print(f"{year} is leap: {is_leap}")Example 1 · Login checks in order
username = "anita"password = "s3cret"attempts = 2
if attempts >= 3: print("Account locked")elif not username or not password: print("Enter both fields")elif password == "s3cret": print("Login successful")else: print("Wrong password")Login successful
What this code does
- The checks that stop the flow come first: a locked account, then missing fields.
- Both fields are filled and the password matches, so the third branch runs.
This clean boolean expression avoids nested if ladders.
Income tax slab calculation
Calculate tax based on progressive income brackets using clean elif ladders.
income = 850000if income <= 250000: tax = 0elif income <= 500000: tax = (income - 250000) * 0.05else: tax = (250000 * 0.05) + (income - 500000) * 0.20print(f"Tax: {tax}")Example 2 · FizzBuzz for one number
num = 15
if num % 15 == 0: print("FizzBuzz")elif num % 3 == 0: print("Fizz")elif num % 5 == 0: print("Buzz")else: print(num)FizzBuzz
What this code does
- Checks divisibility by 15 first (both 3 and 5).
- Prints 'FizzBuzz' since 15 is divisible by both 3 and 5.
Each tier calculates incremental tax for amounts within that bracket.
Finish up
Mark complete & continue
Next: Must-do problems: Conditional Statements