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

Crew Quest

Crew Quest: Routine Remix
// Same output, cleaner code
⏱ about 15 min

Crew Quest: Routine Remix

Raven writes the crew's morning routine on a long scroll. Wake up, wash face, brush teeth. Check the water, write it down. Check the oats, write it down. Check the fuel, write it down.

"What do you notice?" she asks. Rocket laughs. "Check something, write it down. Three times! That should be a function."

Nova writes check_and_record(item) on a sticky note and sticks it on the scroll. "Now the routine is shorter, and it does the same thing," she says.

  1. Read a long program chunk by chunk. Trace loops with a trace table.
  2. Repeated chunks are places for bugs to hide.
  3. The parts that change become parameters. The parts that stay become the function body.
  4. Comments start with #. Good names help people read code.
  5. After a refactor, the output must match exactly.
β—‡ ROUTINE REMIX
Together, write a family routine as a list of steps, like setting the table.
Circle the steps that repeat with small changes.
Give the repeated steps one function name with a parameter.
Rewrite the routine using your function.
Act it out both ways. Does it still do the same thing?
def check(item, count):
    if count < 5:
        return item + " low"
    return item + " ok"

print(check("water", 8))
print(check("oats", 3))
First line on the screen
Second line on the screen
BONUS RECAP
  • Read the question.
  • Tap your answer.
What is refactoring?
Which symbol starts a comment?
After a refactor, what should the output be?

Wonderful remixing! Next week is launch day, and you will graduate as a Crew Engineer.

← Friday