Kai the pilot opens the station report to change the low warning from 20 to 25. "What is n? What is t? Where is the 20?" Kai asks. "I see it three times."
Rocket blushes. He knows what everything means, but nobody else does. Raven asks, "What do you notice? Code is read by people, not just by Python."
Nova shows a sticky note glowing beside one line. "Would you like a hint? Python lets you leave notes that it skips."
A good name says what a value means. amount is clearer than n.
A number that appears in many places can live in one variable instead. Then you change it in one spot.
A comment starts with #. Python skips everything after the # on that line.
# A supply is low below this many units.
low_line = 20
def total_of(amounts):
# Add up every number in the list.
total = 0
for amount in amounts:
total = total + amount
return total
oats = [4, 6, 2]
if total_of(oats) < low_line:
print("Low on oats") # shows only when oats are low | The screen shows |
|---|
| Low on oats |
| Statement | True or false? |
|---|---|
| Python runs the words after a #. | ? |
| A comment can sit at the end of a line of code. | ? |
| Kai can change the warning in one place: low_line. | ? |
| Renaming n to amount on every line changes the output. | ? |
# Fuel check for the rover
tank = 90 # units in a full tank
spare = 18 # units kept for safety
per_trip = 12 # units one trip uses
trips = 5
usable = tank - spare
need = trips * per_trip
print("Usable:", usable)
print("Need:", need)
print("Enough?", usable >= need) | Statement | True or false? |
|---|---|
| Comments change what the program shows. | ? |
| A comment can explain what a number means. | ? |
| Renaming tank to full_tank on every line keeps the output the same. | ? |
| print("# hi") shows # hi on the screen. | ? |
Your code is kind to readers now! Tomorrow you will put the whole refactor together.