Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Numbers and Arithmetic
Python has two kinds of number you will meet constantly:
3, -7, 03.5, -0.25, 2.0The operators are what you would expect, with one that surprises everyone:
7 + 2 # 9 7 - 2 # 5 7 * 2 # 14 7 / 2 # 3.5 <- always a float, even when it divides evenly 7 ** 2 # 49 <- power, not ^
/ always gives a float. 6 / 3 is 2.0, not 2. That is worth remembering because it is a common source of confusion later.
Your task:
Write average(a, b, c) that returns the mean of three numbers.
The mean is the total divided by how many there are, so for 3, 4, 5 it is (3 + 4 + 5) / 3, which is 4.0.
Example Tests
Three consecutive integers average to the middle one
Input: {"a":3,"b":4,"c":5}
Expected: 4
The result is a float even when it comes out even
Input: {"a":2,"b":4,"c":6}
Expected: 4
Works when the answer is not a whole number
Input: {"a":1,"b":2,"c":2}
Expected: 1.66667
def average(a, b, c):
"""
Return the mean of three numbers.
Args:
a, b, c: numbers (ints or floats)
Returns:
Their mean, as a float.
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests