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)
- In the above example, we first define a function called
sumwith a parameter
- We initialize the variable
totalwhich we will use as our running sum.
- For each number in the list, we add that number to the running sum
- At the end of the function, we return the running sum.
- After the function, we create,
n, a list of numbers.
- Finally, we call the
sum(numbers)function with the variable
nand 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
Ignore whether or not the item you’re billing for is in stock.Note that your function should work for any food list.