The Total
Now for the most important number on the receipt: how much the whole order costs. It's the sum from the lesson on loops: a variable that starts at 0, and a loop that adds each value to it.
prices = [4, 8, 15]
total = 0
for price in prices:
total += price
print(total)27For a list of numbers, sum(prices) would do the same. But the cart is a list of dictionaries, and what you need to add up are the prices of its lines. You already have a function that computes them, line_total(), so the new function can simply call it. One function can use another.
Watch the indentation of return: it belongs after the loop, at the same level as for. Indented inside the loop, it would end the function in the very first pass, and the total would be just the price of the bread.
Instructions
Under line_total(), write a function cart_total(items) that goes through items in a loop, adds up the prices of their lines (with line_total()) and returns the sum.
At the end of print_receipt(), after the loop (not inside it), store the result of cart_total(items) in a variable total and print it: Total: $36.
Start programming for free
3/7