Good! Now you’re going to need to know how much you’re paying for all of the items on your grocery list.

def sum(numbers): total = 0 for number in numbers: total += number return total n = [1, 2, 5, 10, 13] print sum(n)
  1. In the above example, we first define a function called sum with a parameter numbers.
  2. We initialize the variable total which we will use as our running sum.
  3. For each number in the list, we add that number to the running sum total.
  4. At the end of the function, we return the running sum.
  5. After the function, we create, n, a list of numbers.
  6. Finally, we call the sum(numbers) function with the variable n and print the result.



Define a function compute_bill that takes one argument food as input.

In the function, create a variable total with an initial value of zero.

For each item in the food list, add the price of that item to total.

Finally, return the total.

Ignore whether or not the item you’re billing for is in stock.Note that your function should work for any food list.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?