Space Python Loading Python…

Edge cases

Write the codeMedium 2 stages

The summary on the engine dashboard uses a small mean() function. It works on the usual lists. Nobody has tried it on an empty pass, a pass with one reading, or the cryogenic sensors, whose readings are below zero.

Write checks for the ordinary cases, then for the edges, where real programs break.

Below the function, write assert checks for an ordinary list of readings and for a list with only one value. Include a list whose mean isn't a whole number, such as [1, 2]. When they all hold, print:

All checks passed

Leave the function exactly as it is.

Checks that pass
Checks that catch mistakes

Finish stage 1 to unlock this stage.

Now the edges. Add checks that an empty list gives None (compare with is None), and that lists with negative values give the right mean, for example [-2, 2], whose mean is 0. Keep the passed line at the end.

The hidden tests break the empty-list check and the handling of negative numbers.

Checks that pass
Checks that catch edge mistakes
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:].