Debugging Conditionals
The program below is meant to print one label for a day's UV index. Readings 0 to 2 are low, 3 to 5 moderate, 6 and 7 high, and 8 to 11 very high, and any other number is invalid. That is a simplified scale: the published one calls 11 and above extreme. This lesson traces inputs through it to find its bugs one at a time, and repairs each.
uv = int(input())
if uv < 0 and uv > 11:
print("invalid")
if uv <= 2:
print("low")
if 5 <= uv <= 3:
print("moderate")
if uv >= 6 or uv <= 7:
print("high")
if uv > 8:
print("very high")
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Given 4, Python runs it without an error message and prints high, although 4 is a moderate reading. Run the program above and type 4 to see high printed. As the lesson finds each bug, repair it in the program and run it again with the reading the lesson traces.
Bounds in order
Python reads the moderate test,
5 <= uv <= 3, as 5 <= uv and uv <= 3, and at 4 its first part is False. No number is both 5 or more and 3 or less, so moderate never prints.The band is 3 to 5, so the repaired line is
if 3 <= uv <= 5:. Then 4 prints moderate, and high after it.Never true, always true
The high line tests
uv >= 6 or uv <= 7, which is True at 4. It's True for every number, since a number below 6 is also at most 7, so high always prints.The invalid line,
uv < 0 and uv > 11, fails the opposite way. No number is below 0 and above 11 at once, so invalid never prints.The high band repairs to
6 <= uv <= 7, and 4 now prints moderate alone.Negating a condition
A valid reading satisfies
uv >= 0 and uv <= 11, so the invalid line was meant to test not (uv >= 0 and uv <= 11). To remove the not, the programmer negated each part and kept the and. At -3 that line is False, and the program prints low.By the first law, the invalid test is
uv < 0 or uv > 11.Separate ifs and a chain
With the invalid line repaired, -3 prints invalid and then low, although each condition repaired so far is right on its own. The five ifs each check their condition, whatever the earlier ones found, and at -3 both the invalid test and
uv <= 2 are true.The labels are alternatives, so they belong in one chain, which prints exactly one label. The moderate and high lines keep their repaired two-sided tests.
uv = int(input())
if uv < 0 or uv > 11:
print("invalid")
elif uv > 8:
print("very high")
elif 6 <= uv <= 7:
print("high")
elif 3 <= uv <= 5:
print("moderate")
else:
print("low")
Now -3 prints invalid alone. The invalid test goes first, since 15 would otherwise pass the very high test.
That is the general rule for a chain. When two tests can both be true for one reading, only the first one's branch runs, so the more specific test goes above the other.
Testing the boundaries
The label changes between 2 and 3, between 5 and 6, and between 7 and 8, and the valid range ends at 0 and 11. None of the readings traced so far lies on an edge, so they can't show a mistake there.
| Input | Meant to print | Chain prints |
|---|---|---|
| -1 | invalid | invalid |
| 0 | low | low |
| 2 | low | low |
| 3 | moderate | moderate |
| 5 | moderate | moderate |
| 6 | high | high |
| 7 | high | high |
| 8 | very high | low |
| 11 | very high | very high |
| 12 | invalid | invalid |
Nine rows agree. At 8,
uv > 8 is False, and 8 is outside the high and moderate bands too, so the reading falls through to else and the chain prints low. The readings 9, 10, and 11 all pass the very high test, so only the boundary itself shows the bug. With that test repaired to uv >= 8, the chain prints the expected label for all ten inputs.