Space Python Loading Python…

Status message crash

Fix the codeEasy 3 stages

The flight computer shows the current altitude in a status line. Someone wrote the line by joining text and the altitude with +, and instead of a status, the console shows an error.

Predict what happens, see the crash, find out why, fix it without changing the message, then write a status line of your own.

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.

This program crashes, and a program that crashes can't be checked. After you run it and see the error, put a # in front of the print line, then check: you'll see your prediction next to what the original line did.

Predict, then run

Finish stage 1 to unlock this stage.

Investigate. Store the name of the error Python reported in error_name, as text, and the type of altitude_km in altitude_type.

Modify. Fix the print line without changing the message or the variable: turn the number into text where you join it, with str(). It must print:

Altitude: 408 km
Read the error and the type
Fix the status line

Finish stage 2 to unlock this stage.

During docking, the console shows the approach speed and whether the ship has docked. Build the line by joining text and the two values with +, converting each value with str():

Approach speed: 0.12 m/s, docked: False
Print the docking status
Leave the sensor values as they are
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:].