Space Python Loading Python…

Readings that won't add

Fix the codeEasy 3 stages

The fuel tank sensors send their readings to the flight computer as text, the way most data arrives from files, forms and networks. The code that adds them up looks right, but the total it prints makes no sense.

Predict what it prints, find out why, fix it, then handle readings with decimals yourself.

Don't run the code yet. Read it and decide what it will print. Write your guess in prediction, at the top, between the quotes. Then run it and compare.

Any guess passes this stage: the point is to think before you run. After you check, you'll see your prediction next to what Python printed.

Look carefully at how the two readings are written.

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Store the type of the readings in reading_type: int, float, str or bool.

Modify. The readings arrive as text, so leave the two lines that set them as they are. Convert them where you add them up, with int(), so the program prints:

200
Find the type of the readings
Add the readings as numbers

Finish stage 2 to unlock this stage.

The cabin pressure sensors send readings with decimals, again as text. Add up all three and print the total with one decimal place:

Total: 301.5 kPa

Try int("101.3") first and see what happens: int() only reads whole numbers. For decimals you need float().

Print the total pressure
Keep the readings as they arrived
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:].