← Back to course
Code Crew G6+ / Week 02 / Friday
5/6
Week 02 Β· Variables and input

Friday

Trip time calculator
// Programs that remember and ask
⏱ about 30 min

Friday: Trip time calculator

The crew is planning a long drive to the test range. Everyone is asking the same question: how long will it take?

Rocket rubs his hands together. "I have an idea!" he says. "A calculator that asks for the distance and the speed, then works out the time. We have every piece we need."

Raven writes the pieces on the whiteboard: ask, convert, calculate, show. "What do you notice?" she asks. "It is four small steps, and we learned each one this week."

Nova hovers near the board. "And when a user types something unexpected," she says, "Python will tell us. Let's read that message calmly when it comes."

Rocket starts typing, one step at a time, testing as he goes.

The program

# trip_time.py
# Works out how long a trip takes.

print("Trip time calculator")
distance = float(input("Distance in km: "))
speed = float(input("Speed in km per hour: "))

hours = distance / speed
minutes = hours * 60

print(f"The trip takes {hours:.2f} hours.")
print(f"That is about {round(minutes)} minutes.")

round() rounds a number to the nearest whole number. Here is a run where the user types 150 and 60:

Trip time calculator
Distance in km: 150
Speed in km per hour: 60
The trip takes 2.50 hours.
That is about 150 minutes.
PUT THE FOUR PARTS OF THE PROGRAM IN ORDER
  • ?Show the title
  • ?Show the answers
  • ?Ask for the distance and the speed
  • ?Work out the hours and minutes
WHY THIS EXERCISEMost small programs follow this shape: get input, calculate, then show output.
The user types 100 for distance and 50 for speed. How many minutes does it show?
WHY THIS EXERCISEhours is 100 / 50 = 2.0, and 2 hours is 120 minutes.

When the user types something unexpected

If the user types ten instead of 10, float() cannot turn the word into a number. The program stops with a ValueError, which means the value had the right type but could not be used.

Another common error is a TypeError, like trying to add a string and a number:

>>> float("ten")
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
ValueError: could not convert string to float: 'ten'
>>> "Fuel: " + 50
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
NAME THE ERROR
  • Read the question.
  • Tap your answer.
The user types five when int(input()) expects digits. Which error appears?
Which line fixes "Fuel: " + 50?
At your computer
1. Type trip_time.py in a new file and save it.
2. Run it with 150 and 60. Check that you see 2.50 hours and 150 minutes.
3. Run it with 100 and 50. Was your prediction right?
4. Run it again and type ten for the distance. Read the last line of the error.
5. Add one more line that shows the speed in km per minute.

Practice: a fuel calculator

Here is a second calculator with the same shape: ask, convert, calculate, show. Read it and predict before you run it.

# fuel_calc.py
distance = float(input("Distance in km: "))
rate = float(input("Fuel units per km: "))
fuel = distance * rate
print(f"Fuel needed: {fuel:.1f} units")
Distance in km: 120
Fuel units per km: 0.75
Fuel needed: 90.0 units
PREDICT AND DEBUG
  • Read the question.
  • Tap your answer.
The user types 200 and 0.5. What does it show?
Why does this program use float() and not int()?
The user types 12 km for the distance. What happens?
Distance 80, rate 1.5. The program shows Fuel needed: ____ units
Distance 10, rate 0.25. The program shows Fuel needed: ____ units
StatementTrue or false?
A ValueError means a value could not be used, like the word ten for float().?
"Score: " + 5 works fine in Python.?
round(2.6) gives 3.?
Most small programs get input, then calculate, then show output.?
WHY THIS EXERCISEMixing a string and a number with plus is a TypeError. An f-string is the safe way to mix them.

You built a real calculator from four small steps. In week 10 you will learn how to catch errors like ValueError so the program can ask again.

← Thursday