← Back to course
Code Crew G4 / Week 01 / Friday
5/6
Week 01 Β· Values and print

Friday

Build a fuel report
// Real Python, one line at a time
⏱ about 15 min

Friday: Build a fuel report

The station needs a fuel report before tonight's test flight. Rocket writes it fast. He tries to join the string "Fuel: " with the number 40, and Python stops with an error.

"Hmm," says Raven. "What do you notice? One side is a string and one side is a number."

Rocket groans, then grins. "We can improve it!" Nova flies a slow loop around the screen. "Would you like a hint? print can show more than one thing if you use commas."

Commas in print

You cannot join a string and a number with +. Python stops with an error.

Instead, put a comma between them inside print. print shows each one and puts a space between them.

crates = 12
crates = crates + 3
dock = "Bay 2"
print("Crates:", crates)
print("Dock:", dock)
The screen shows
Crates: 15
Dock: Bay 2

Notice the space after the colon. The comma added it for us.

Rocket adds a line at the end: print("Total:", crates + 5). What number shows after Total:?
WHY THIS EXERCISEYou can do math right inside print. Python works out the value, then shows it.
PUT THE LINES IN ORDER SO THE SCREEN SHOWS TANKS: 6
  • ?tanks = 2
  • ?tanks = tanks * 3
  • ?print("Tanks:", tanks)
WHY THIS EXERCISEOrder matters. A print line only shows what is in the box at that moment.

Debug it

Rocket wants this program to show Oxygen: 30. It shows Oxygen: 25 instead.

oxygen = 25
print("Oxygen:", oxygen)
oxygen = oxygen + 5
FIND THE FIX
  • Read the question.
  • Tap your answer.
How can Rocket fix the program?
At the computer
This is never required. Paper works great.
If you like, a grown-up can install Python from python.org.
Then open the program called IDLE and type today's fuel report.

Practice lab: a hatch report

pods = 4
pods = pods + 2
name = "Hatch A"
print("Pods:", pods)
print("Name:", name)
print("Double:", pods * 2)
What number shows after Pods:?
What shows after Name:?
What number shows after Double:?
COMMAS OR PLUS?
  • Read the question.
  • Tap your answer.
fuel = 40 runs. Which line shows Fuel: 40 with no error?
What does print("A", "B") show?
Why does print("Fuel: " + 40) stop with an error?
StatementTrue or false?
A comma in print puts a space between the values.?
print("Tanks:", 3) shows Tanks:3 with no space.?
Python can do math inside print.?
A print line shows what is in a box at that moment.?
WHY THIS EXERCISEKnowing exactly what print does makes your reports neat and correct.
Trace this: crates = 5, then print("Crates:", crates), then crates = crates + 1. What number shows?
WHY THIS EXERCISEprint shows the value at that moment. The change on the last line comes too late.

Week one is done, Crew Engineer! You can print, store, change and join values. Next week you will keep many values in one list.

← Thursday