Making a Purchase


#1

why my code is not working ?? the output says "Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 30 instead of 10.5"

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 x in food:
        item = prices[key] + total
        total = total + item
    return total

#2

With the item = prices[key] + total and total = total + item you are basically adding everything twice. Try to limit that to just adding the prices[key] to total using the += assignment operator. That way it adds the value of each item in sequence and only once.


#3

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 x in food:
        total += prices[x]
    return total

Worked for me.
prices[x] = prices for each food


#4

Please do not post complete code as an answer, it takes away from actually learning what makes the code work.


#5

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