Stocking Out


#1


Hi all, im having problems with Stocking out exercise 12. of a day at the supermarket.


i've looked at other answers to the same topic but the code looks the same to mine. i get the error message: Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7

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


#2

Set your return statement on the same indentation level as your for loop and your code should work, :slight_smile:

for x in something:
    some code
return var

#3

thanks i overlooked the simple stuff xD


#4

shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total

worked for me :smiley:


#5

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