Error in lesson (Stocking Out)


#1

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):
total=0
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

print(compute_bill(shopping_list))


#2

Yes.

stock[i] = stock[i] - 1

or the short form,

stock[i] -= 1

#3

@mtf

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}


#4

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


#5

@mtf

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

Thanks' for your help i got this.

Regards,
TBIKU


#6

@mtf

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

Regards,
TBIKU


#7

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

#8

@mtf
thanks' boss

My regards,
TBIKU


#9

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