Stocking out


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#


Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7


please i need help code isnt working


 def compute_bill(food):
    total = 0
    for item in food:
        if stock[key] > 0:
            total = total + prices[key] 
            stock[key] = stock[key] - 1
    return total


#2

here:

        if stock[key] > 0:
            total = total + prices[key] 
            stock[key] = stock[key] - 1

where does key come from?


#3

i dont understand your question


#4

you use key in your code, where did you define key? What do you think key does?


#5

a python dictionary has a key: value pair and items in dictionaries are accessed through keys


#6

yes, a dictionary has keys and values. But what variable contains your keys? Because keys is currently a undefined variable (yes, its a term we describe keys in dictionary, but you need an actual key to retrieve value of dictionary)


#7

please can help me with the problem regarding my code


#8

well, you loop over food which is a list containing products, which are the keys for your dictionary (to get price and reduce stock). So, you should use item, not keys


#9

Thanks for the assistance. Code worked perfectly!!


#10

do you also understand why? That is quit important


#11

Yes i do. Thanks again


#12

2 posts were split to a new topic: Stocking out


#14

2 posts were split to a new topic: Stocking out


#16