10.11: Making a purchase: I cant find the answer. Any help plz?


#1

HERE IS MY CODE

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 food in food:
total+=prices(food)
return total
print total

can someone tell me where I am screwing up?

please put it in coding format and thanks for helpin' a guy out


#2

def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total

You don't need the print function since you already used return. Another thing is you need to use the item instead of just (food). In addition, you also need to use square brackets. Lastly, make sure it is indented properly.

1st line: no ident.
2nd line: 1 ident.
3rd line: 1 ident.
4th line: 2 idents.
5th line: 1 ident.

I think that's everything. Hope it helps!


#3

I wrote it exactly this way, and it doesn't work for me :frowning: It says: compute_bill(['apple']) resulted in a Type Error: unhashable type: 'list'.


#4

I'm a bit rusty, why [] brackets?


#5

This is my code worked fine for me

# Write your code below!

def compute_bill(food):
total = 0
for n in food:
if stock[n] > 0:
total = total + prices[n]
stock[n] = stock[n] - 1
return total


#6

@nico1057

You need [] brackets because you are creating a list, not a dictionary. If it were a dictionary, it would be {}


#7

@pinocio

Your code is good, and would work, but the if statement is not required, in fact, it makes the code fail. For anyone who needs the correct code:

# Write your code below!
def compute_bill(food):
    total = 0
    for n in food:
        total = total + prices[n]
        stock[n] = stock[n] - 1 
    return total

Hope this helps. Good luck and happy coding.


#8

That was pretty spot on! In my experience I found that putting in the stock[n] thing just makes things complicated. This worked for me. Sweet and simple.

def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total


#9

i think you need the if statement just to make sure that ur stock is not 0 just as the questionary requires


#10

ok sorry u will need the if statement in the following 10/12 Stocking out lesson :smile:


#11

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 num in food:
total += prices[num]
if stock[num] > 0:
stock[num] -= 1
return total

just copy/paste , mind spaces , its working for me....good luck every1