← Back to course
Code Crew G4 / Week 11 / Thursday
4/6
Week 11 Β· Refactor

Thursday

Names and comments
// Same output, cleaner code
⏱ about 15 min

Thursday: Names and comments

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."

Clear names

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.

Comments

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
StatementTrue 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.?
WHY THIS EXERCISEGood names and comments are a gift to the next person who reads your code.
PICK THE BETTER NAME
  • Read the question.
  • Tap your answer.
A variable holds how many crew are on board. Which name is clearest?
Which line is a comment?
Kai sets low_line = 15. Does the program above still show Low on oats? Type yes or no.
WHY THIS EXERCISEWith one variable for the line, testing a new rule takes one small change.
Try it
Find a program from an earlier week.
Write one comment that explains a line.

Practice lab: a rover fuel check

# 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)
What number follows Usable:?
What number follows Need:?
What follows Enough?
READABLE CODE
  • Read the question.
  • Tap your answer.
Which name is clearest for units kept for safety?
What does Python do with # units in a full tank?
trips changes to 7. What does the Enough line show?
StatementTrue 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.?
WHY THIS EXERCISEA # starts a comment only when it is outside a string.
spare changes to 30. What number follows Usable:?
WHY THIS EXERCISEOne clear variable means one small change to test a new plan.

Your code is kind to readers now! Tomorrow you will put the whole refactor together.

← Wednesday