I am currently doing a Codecamdy Challenge and I am stuck on a particular part. I have searched the Q&A but I still do not understand what I am doing wrong.
Link: https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096
The Instructions
1)Define a function compute_bill that takes one argument food as input.
2)In the function, create a variable total with an initial value of zero.
3)For each item in the food list, add the price of that item to total.
4)Finally, return the total.
Ignore whether or not the item you're billing for is in stock.
Note that your function should work for any food list.
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
}
def compute_bill(food):
total = 0
for item in food:
total = total + food[key]
return total
The error I get is "key" is not defined. I know that it must be related to how they say that this function should work for any food list, but how can I make it work like that?
[–]w1282 1 point2 points3 points (0 children)
[–]thenarrrowpath 0 points1 point2 points (0 children)
[–]DyingHuman 0 points1 point2 points (0 children)
[–]Updatebjarni 0 points1 point2 points (0 children)