25 XP

8. Add up a list

Beginner lists loops

Write a function total(numbers) that takes a list of numbers and returns their sum. Don't use Python's sum(): add them up yourself with a loop.

The numbers can be negative or decimals. If the list is empty, there is nothing to add: return 0.

Example 1

Inputnumbers = [1, 2, 3, 4]
Output10
Explanation1 + 2 + 3 + 4 = 10.

Example 2

Inputnumbers = [10, -3]
Output7
Explanation10 + (-3) = 7.

Example 3

Inputnumbers = []
Output0
ExplanationAn empty list adds up to 0.

Constraints

  • numbers has 0 to 100 items.
  • Each item is a whole number or a decimal from -1000 to 1000.

Submit also runs 4 more tests you can't see.

Practises Lists (lesson 9).

Code
def total(numbers):
    """Add up a list of numbers, without sum().

    numbers: list of int or float; may be empty
    Returns: int or float, their sum (0 for an empty list)
    """
    # Write your code here
    pass