11.Making a purchase


#1

hiii, i'm working on exercise 11 from 'A day at the supermarket' and i need help!

it doesn't wanna run
what am i doing wrong?

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

#3

Okay I'm not sure why this is but I spent like 20 minutes trying to figure it out and you have to use

for item in food:
total = total + prices[item]
return total


#4

@swanlake1925 This code does the same thing.

Your code is otherwise right, but you should use += method when you add something to total.
It should look like

 for item in food:
    total += prices[item]

And after it, return the result.


#5

Thank you so much!!!:grin:


#7

Thank you very much :grin:


#8

No problemo! Hope this helps in the future!


#10

thank you very much! :blush:


#11

Hi, actually I take your code and try to run, but it doesn't, can anybody tell me what I'm doing wrong?

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


#12

I reply myself, it's just the way I structure the code, sorry and thanks


#13

can you put the link of the exercise? please
it's better if you put it


#14

You used for item in food
Since you are using prices[key], it does not work as it is different to what is used in the for statement (i.e key)


#15

Hi guys, can anyone explain why total += prices[item] returns the right result?
How does the function know that it has to multiply the number in the price list with the number in the stock list?
Shouldn't it be total += prices[item]*stock[item] ?


#16

The function isn't looking at the stock at all yet, it's just adding up the prices of your shopping list and telling you the total, not the cost of the entire stock of each item in the list.


#17

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