11/13 - How to add food to the total


#1

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!
Cheers

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


#2

That worked for me

Write your code below!

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


#3

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


#4

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


#5

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

#6

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


#7

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

#8

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 !!!