Space Python Loading Python…

Prints instead of returns

Fix the codeEasy 3 stages

The thermal model needs engine temperatures in kelvin, so someone wrote a small conversion function. It even seemed to work: the right number appears on the screen. But the model receives nothing.

Predict what the program prints, find out what the function really gives back, fix it, then write another conversion that the crew displays can use.

Don't run the code yet. Read it and decide what it will print on its last line. 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.

Two things print here: the function, and the line below it. What does engine_k hold?

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Print what to_kelvin(412.5) gives back and store it, as text, in returned. Then print type(engine_k) and store the type's name in value_type. Remove any lines you added before you check.

Modify. Make to_kelvin() give its result back, so the program prints only:

ENG-2: 685.65 K
Find what comes back
Return the result

Finish stage 2 to unlock this stage.

Write to_fahrenheit(celsius), which returns celsius * 9 / 5 + 32, and use it to print:

CABIN: 70.7 °F
ENG-2: 774.5 °F

The function returns the number; the prints round it with :.1f.

Print two temperatures
A function that returns
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

Return Statement Basics

return-statement-basics.py
# return hands a value back to whoever called the function
def kelvin_to_celsius(kelvin):
    return kelvin - 273.15

# The value comes back, so you can store it in a variable
venus = kelvin_to_celsius(737)
print("Venus surface:", venus, "C")

# Or use it immediately, inside something else
print("Titan:", round(kelvin_to_celsius(94), 1), "C")

# A function that only prints hands back nothing at all
def show_temp(kelvin):
    print("Reading:", kelvin, "K")

result = show_temp(94)
print("What came back:", result)

# So this would fail, because you cannot subtract from nothing
# print(show_temp(94) - 10)

# return also ends the function on the spot
def coldest_known():
    return 1
    print("This line never runs")

print("Boomerang Nebula:", coldest_known(), "K")
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:].