Making purchase, returning a value that I don't understand why it does

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

Oops, try again. compute_bill([‘banana’, ‘apple’, ‘orange’, ‘pear’]) returned 3 instead of 10.5


i found the problem, its here:

total =+ prices[key]

to update a variable, we can do:

x = x + "added value"

or we can use the shorthand (+=), it seems you attempted the shorthand but swapped the operators around


Hahahaha, thanks a lot, I’ve been trying to figure this out for hours on my own, can’t believe I missed that. Worked straight away, thanks.


Our brain read words (see link i posted above), not letters. Because this is the case, i also suspect that in a way this is related to why we can’t find such small mistakes. We just look at += as a whole, tricking our brain into thinking its right.


A post was split to a new topic: Making a purchase


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