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


#1




So my code is telling me that I'm returning 9 instead of what should be 7, does anyone have any advice as to what I may be doing?


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


#2

@shadowmyth ,

You have ...

if stock > 0:

... which does not specify which item in the stock dictionary you wish to access.


#3

Thanks appylpye! I feel a bit ridiculous for not noticing that before, thanks for the help! :slight_smile:


#4

try
if stock[item] > 0:


#5

this really helped <3


#6

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