Adding Up with a Loop

~8 mincode completion

Write total_of(numbers) that adds up a list of numbers and returns the total. Write the loop yourself rather than calling the built-in . An empty list should return 0.

Examples

Adds up three numbers

Input
total_of([1, 2, 3])
Output
6

Negative numbers subtract from the total

Input
total_of([10, -3, -2])
Output
5

An empty list totals 0

Input
total_of([])
Output
0

Hints

Hint 1

Walk the input once and accumulate as you go.

Hint 2

Watch for this: reset accumulator inside loop.

Requirements

  • numbers: a list of numbers

  • Return Their total. An empty list totals 0.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

3 employers weight this skill

2 frontier labs, 1 AI product company. Top match scores 80.

Python
def total_of(numbers):
    """
    Add up a list of numbers using a loop.

    Args:
        numbers: a list of numbers

    Returns:
        Their total. An empty list totals 0.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.