Space Python Loading Python…

Fix the Fuel Report

Fix the codeEasy 3 stages

Ten minutes to launch, and the fuel report on the main console keeps crashing. The engineer who wrote it is already strapped into the capsule, so it's up to you.

The code is almost right. Press Run, read the error, and fix what breaks. Don't rewrite everything: find the problem and change as little as you can.

The report should say:

Fuel level: 87%

Instead, the program stops with a TypeError. Run it, read the last line of the error, and fix the print() line. Keep fuel as a number: other parts of the ship calculate with it.

The report prints the fuel level
fuel stays a number, and the report uses it

Finish stage 1 to unlock this stage.

The tank sensor now types the fuel in, and the ship always carries 250 litres in reserve. For a reading of 1200 the report should say:

Total fuel: 1450 litres

It crashes again. Fix it so the total is a real sum, whatever the sensor types.

Add the reserve to the sensor reading

Finish stage 2 to unlock this stage.

Last check before launch: how long can the engines burn? The program divides the fuel by the burn rate (litres per second). Burn rates can have decimals, like 2.5, and the time is rounded to one decimal place. For 1200 litres at 30 litres per second:

Engines can burn for 40.0 seconds

There are two bugs this time.

Read burn rates with decimals
Print the burn time, rounded to one decimal place
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Type Conversion

type-conversion.py
# Telemetry always arrives as text, even when it looks like a number
impact_speed_text = "22530"
orbit_change_text = "32.0"

# int() turns a string of digits into a whole number
impact_speed = int(impact_speed_text)
print("DART hit Dimorphos at", impact_speed, "km/h")
print("That is", round(impact_speed / 3600, 2), "km every second")

# float() does the same job for a decimal
orbit_change = float(orbit_change_text)
print("Orbit shortened by", orbit_change, "minutes")

# str() goes the other way. Plus only joins text to text
year = 2022
print("Impact year: " + str(year))
# print("Impact year: " + year)     # delete the hash sign to meet a type error

# int() on a decimal chops off the fraction, it does not round
hours_per_orbit = 11.92
print("int gives", int(hours_per_orbit), "but round gives", round(hours_per_orbit))

# A decimal hiding inside a string needs float() first
raw = "11.92"
print("Converted safely:", int(float(raw)))
# print(int(raw))                   # this one raises a value error

# type() confirms what you are actually holding
print(type(impact_speed_text), type(impact_speed), type(orbit_change))
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].