To me 8.5 seems like the correct answer


#1

Question 12/13 : A day at the Supermarket, Stocking Out
I am getting an ERROR ‘calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 8.5 instead of the correct 7’. To me 8.5 seems like the correct answer. Adding 4 for banana, 1.5 for orange and 3 for pear should be 8.5. Do not understand where i am going wrong. Please see my code below.

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


Day at supermarket - error not understood
#2

Error:

calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 8.5 instead of the correct 7

Did you understand the error message?

call the function with following argument

print compute_bill(['apple', 'pear','banana'])

now what should it return?and what does it return?

for b in stock:

why are you looping through stock?


#3

IT WORKED !
Thank you for pointing it out. I should be looping through ‘food’ ! Else it will always output 8.5 as it gets its values from stock, which is always the 4 values {“banana”, “apple”, “orange”, “pear”}.


#4

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