Making a Purchase


Hi everyone,
below is my code,
I am wondering why it will automatically calculate to 117 (6*4+32*1.5+15*3) instead of 4+2+1.5+3 ?
Does anyone know?

shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3

# Write your code below!
def compute_bill(food):
    for item in food:
        total += prices[item]
    return total
print total


Hi @betablaster91992,

Technically the global variable, total, is undefined here ...

print total

If it is displaying a result without an error, it is probably because Codecademy has cached a value for total in memory from a previous version of your code or from a previous exercise.

Try this instead ...

total = compute_bill(shopping_list)
print total

Be sure not to indent those statements.


I got it, thanks!!! :grinning: