Four broken conditionals. Only one of them crashes — the other three run happily and give you the wrong answer.
This is the debug challenge closest to real programming. Conditional bugs almost never announce themselves; the program just quietly does the wrong thing.
For each one, write down which kind of mistake it is, what is actually wrong, and the corrected line. Trace it in your head first, then run it and see whether you were right.
Symptom: refuses to run at all. Python complains about line 3.
age = 18
if age = 18: print("You are exactly 18.")The fix is a one-character change. Ask yourself what line 3 is currently telling Python to do, versus what it should be asking Python to check.
Newer versions of Python add a hint to this error suggesting what to use instead. Older ones just say invalid syntax and leave you to it — so do not count on the hint being there.
Symptom: prints It's the weekend!— which looks right, since day 7 is Sunday. But change day to 3 and it still says it's the weekend.
day = 7
if day == 6 or 7: print("It's the weekend!")else: print("It's a school day.")That condition reads like English and does not mean what the English means. Python sees two separate things either side of or — what is the second one, on its own?
Symptom: a score of 95 prints You passed. It never once prints You got an A! for any score at all.
score = 95
if score >= 60: print("You passed.")elif score >= 90: print("You got an A!")Both conditions are written correctly. Nothing is misspelled. The bug is in the order — explain why the second branch can never be reached.
Symptom: a 12-year-old is correctly told they cannot drive, and is then handed the keys anyway.
age = 12
if age >= 16: print("You can drive!")print("Here are the keys.")Nothing here is misspelled either, and Python reports no error. The whole bug is one line's worth of whitespace. This is the kind of mistake that makes indentation worth taking seriously.
Then one more, in a sentence: which of these four would be hardest to find in a 200-line program, and why?