11/13 Making a purchase not iterable error


#1

*****Whenever I run this code I get an error saying
"compute_bill(['apple']) resulted in a TypeError: 'int' object is not iterable"
Why is that please help?**

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


#2

As below is also not working on the apple

def compute_bill(food):
total = 0
for key in shopping_list:
total += stock[key] * prices[key]
return total

compute_bill(shopping_list)


#3

lemme know if you get an answer


#4

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


#5

Well there are several go to ways to do this,

Normal for loops

def compute_bill(food):
    cost = 0
    for x in food:
        cost += prices[x]
    return cost

Or a list comprehension

def compute_bill(food):
    return sum([prices[price] for price in food])

#6

For this I think you might be messing up at total += by using a multiplying the two together as was done to figure out the total net worth of the product. In this you need to simply add prices[key] w/ the total and completely ignore stock.


#7

thanks it works perfectly


#8

Do you mind explaining why line four is prices[item] and not prices[value]? I got caught up on this specific detail and haven't been able to find any info on why it's 'item' and not 'value'.


#9

You will see as you go, how much you are implying as you go. Don't give up.

:relaxed:


#10

Anyone know why we add [item] to line four?


#11

because item is used in for loop.


#12

Hey,
I am stuck at that task as well but it says that:
Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

my code looks like this. I tried the answers before as well but it wouldn't work:

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

thanks!


#13

I had the same issue but it was because my return statement was not indented correctly. Remember it is not indented under the for loop but rather the function itself:
def compute_bill(food):
total = 0
for item in food:
total +=prices[item]
return total

no indent:def compute_bill(food):
1 indent: total = 0
1 indent: for item in food:
2 indents: total +=prices[item]
1 indent: return total


#14

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]

here we should let calculate the total and finally it return total.

that's why we intend return statement to for loop not after the total counter

return total

# print (compute_bill(shopping_list))


#15

Why it's prices[item]? Why not food[item]?


#16

Thanks, this thread helped me.