12. Stocking Out HELP WHAT IS WRONG WITH MY CODE


#1



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

WHAS WRONG HELP PLEASE


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 item in food:
        total = 0
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total


#2

just think about the Error measasge......

1 apple, 1 pear and 1 banana resulted in 3 instead of the correct 7

why so?
stock of apple is 0 so it'll not count.
1 pear=3 and 1 banana= 4 so it should be 7

problem comes from here

for item in food:
        total = 0

inside of for...in loop total=0 what does this mean?it remains 0 all the time.so it doesnt effect
here

total += prices[item]

so the price of last item you added will be the result always.


#3

Thank you it was a silly mistake and it stumped me for awhile! thanks for the advice i got it.


#4

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