A day at the supermarket - What is the mistake


#1

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:
            #print item
            total += prices[item] * stock[item]
            #stock[item] -= 1
    return total

print compute_bill(shopping_list)
print stock

What I missed? I have every thing as per your instructions!


#2

total += prices[item] * stock[item]

Problem is shopping_list was not defined amount of every kind of food, so you can know it always is 1. Try to remove * stock[item] or try this:

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
print compute_bill(shopping_list)

#3

I have try that one also but it showing this error.

"calling compute_bill with a list containing 1 apple, 1 pear, 3 oranges and 9 bananas resulted in 43.5 instead of the correct 31.5"


#4

You know, I think 9 bananas is over stock. They give us only one condition is amount of item in stock must be greater than 0. Did you try to add more conditions to limit shopping list? Example: if bananas in shopping_list > bananas in stock -> set bananas in shopping_list = bananas in stock :expressionless: My situation was not like you :expressionless: That code I gave you above work fine for me.


#6

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