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

~7 mincode completion

Python has two kinds of number you will meet constantly:

  • int, a whole number: 3, -7, 0
  • float, a number with a decimal point: 3.5, -0.25, 2.0
  • The 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

    Python
    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
        pass

    Run your code to see results

    ⌘↵ runs against the visible tests

    Loading docs…