11/13 - How to add food to the total


Hey guys,

I am confused about how to add food to the total when not specifying a specific list.
The problem that will appear below is: a string (food) cannot be added to an integer (total)
Help is very much appreciated!

def compute_bill(food):
total = 0
for food in food:
return total = total + food


That worked for me

Write your code below!

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


Thanks a lot, that worked for me as well!
However, when rerunning the exact same syntax, there is an error :frowning:


What error would that be. or do u know care about it


your problem is that you can not use the same name for two different objects.

you have written

> for *food* in food       # I have put asterisks around the problem in the code

you cannot have "food" to mean two different objects.

try re-naming to

> for *item* in food


I tried all these approaches but only had success with the following:
total = [ ]
for key in prices:
ns = prices[key]*stock[key]
print sum(total)


just found the problem...

the return command only prints a value to the memory. the total = total + food must be on its own line, previous. as in:

> total = total + food                           #or total += food
> return total


wrong syntax in the loop; review lesson in dictionaries; in the loop for food in food, not to be confused, better screver for m in food,
then m is the key element in the dictionary and if we want his value syntax is: food [m], and in its total = Total code + food [food]; contuto not use variables commesmo name !!!