Error in lesson (Stocking Out)


Dear Sir,
I got struck in this below lesson. In this code i just did loop for adding prices by passing stock item from if condition expression but for reducing -1 from stock i can't do because i can't return total and stock at a same time. So, do i need to make new variable for stock or what??

My Code is below:

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 i in food:
if stock[i]>0:

        total= total+prices[i] 
        **stock[i]-1**------------------------------------->is this wrong? how we can return value of stock

return total




stock[i] = stock[i] - 1

or the short form,

stock[i] -= 1



Still i am getting below error after all these code run.

Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}


Your call to the function is modifying the expected values. Comment that line or remove it.



Ahh so this project want me to just calculate the values only....

Thanks' for your help i got this.




Boss is there any way to call [key of dictionary] like we can all it's value by saying dict[key]



It should come up shortly in a lesson. The dict class has three such methods,

dict.items()    => a list of tuples, [(key, vlaue)]
dict.keys()     => a list of keys
dict.values()   => a list of values


thanks' boss

My regards,


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