The crew has a new rule: five units or fewer means a shelf is low. Rocket runs his supply check. The shelf with exactly five units says ok.
"That shelf should be low," Raven says. "What do you notice about the comparison sign?"
Rocket looks at it for a long time. "It says less than. But five is not less than five." Nova glows softly. "Great discovery! Would you like a hint? One more symbol can make the tie count."
A number right on the line, like 5 in this rule, is called an edge case.
Edge cases are where many bugs hide. Always test them.
shelves = [6, 5, 2]
for s in shelves:
if s < 5:
print(s, "low")
else:
print(s, "ok") | The screen shows |
|---|
| 6 ok |
| 5 ok |
| 2 low |
Rocket typed if s = 5: by mistake. Python stops before running anything and shows a SyntaxError.
A single = stores a value. Inside an if, you need == to ask a question.
| Line | Will Python accept it? |
|---|---|
| if s == 5: | ? |
| if s = 5: | ? |
| if s <= 5: | ? |
| if s < 5 | ? |
temps = [18, 25, 21]
for t in temps:
if t > 22:
print("warm")
elif t > 20:
print("just right")
else:
print("cool") The rule is over 90 is great and over 60 is good. Every score below says good.
scores = [95, 70, 85]
for s in scores:
if s > 60:
print(s, "good")
elif s > 90:
print(s, "great")
else:
print(s, "keep going") | Score in the fixed program | Does it show good? |
|---|---|
| 70 | ? |
| 95 | ? |
| 61 | ? |
| 60 | ? |
Week four is done! Your loops can choose. Next week, you will look closely at strings.