Making a purchase


#1



Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2


It seems like my code is in line with what other people say is correct but I keep getting this error message!


def compute_bill(food):
    total = 0
    for item in prices:
        total += prices[item]
    return total


#2

Hi @catsatemyface,

With this loop header, your code adds up the cost of all the items in the prices dictionary ...

for item in prices:

Instead of prices, use the function parameter, food, as the source of the keys in the loop header.


#3

i have the same problem pliiiz i need the help ...this is my code ..

Write your code below!

def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total
print compute_bill(shopping_list)

where is the prooooooooblem


#4

@hydra_95 ,

Your problem is a different one. Is this statement indented too much, which would make it part of the for loop? ...

return total

#5

thank you appylpye...i have overhauled the problem :grin:


#6

Thanks that helped me out too.
I have a quick question though.
How does the code know what the "food" paremeter is, if it's not defined anywhere? How does it know that the parameter food contains banana, apple, orange, pear?


#7

The compute_bill function does not have any information concerning what food refers to until someone calls the function.

If a programmer does this ...

shopping_list = ["banana", "orange", "apple"]
print compute_bill(shopping_list)

... then the function's food parameter will refer to the shopping_list argument, and the function will process that list accordingly.

You can experiment with it to find out what happens. However, be aware that in Exercise 12. Stocking Out, Codecademy does not want you to call the compute_bill function. You can can call the function if you want, but within that exercise, comment out the calling statement in order to pass.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.