Space Python Loading Python…

Statistics module

Fix the codeEasy 2 stages

The temperature summary carries its own mean() and median(). They work, mostly. Somebody once got the median wrong for an even number of readings, and nobody has written a standard deviation yet because it looked like work.

Python already ships all of these, tested, in its standard library. Use them instead.

Delete the hand-written mean() and median(), add import statistics at the top, and print the same two lines with statistics.mean() and statistics.median():

Mean: 418.8 C
Median: 413.8 C

The output doesn't change: that is the point of a refactor. The check looks for the module and makes sure your own functions are gone.

Same results
From the module

Finish stage 1 to unlock this stage.

Now there are three passes. For each, print the mean, the median and the standard deviation, statistics.stdev(). Pass 2 has a single reading and pass 3 none; catch statistics.StatisticsError and print the module's own message for those:

Pass 1: mean 418.8 C, median 413.8 C, stdev 21.1 C
Pass 2: stdev requires at least two data points
Pass 3: mean requires at least one data point
Every pass handled
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

Import System Modules

import-system-modules.py
# A module is a toolbox someone else already built and tested for you

# Plain import brings in the whole module
import sys
import platform
import math

# from ... import brings in just one tool
from math import sqrt

# as gives a module a shorter nickname
import random as rng

# With a plain import: module name, a dot, then the tool
print("Python version:", platform.python_version())
print("Running on:", platform.system())
print("Python lives at:", sys.executable)

# math carries constants you would not want to type from memory
radius_km = 6371
print("Earth circumference:", round(2 * math.pi * radius_km), "km")

# sqrt arrived on its own, so it needs no dot in front
print("Square root of 9:", sqrt(9))

# rng is simply a nickname for random
print("Random sol of the Martian year:", rng.randint(1, 687))
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:].