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

#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
}

def compute_bill(food):
total = 0
for key in food:
total =+ prices[key]
``````

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

#2

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

#3

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.

#4

http://www.douglastwitchell.com/scrambled_words.php

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.

#5

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

#6

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