That food thing in the loop makes no sense (11. Making Purchase)


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


the code works fine, but i dont understand, where food comes from? i mean i have my shopping_list, but i do loop through a list with the name food? which i dont see created somewhere?


well i expected that i have to use the shopping_list or create a list with teh name food. would be nice if somebody can explain me that.


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


#2

You aren't computing any bills, so no list of food is required. To use your function, you would need to have a list of food.


#3

so when i write the same function in python on my computer, i would really need to setup a food list to make it working right?

well as i started this lesson, this really kept me thinking( and busy) outside of the instructions.


#4

If you want to run your function then that's because you have a food-list and want to know what the bill would be

If you want to run the function anyway, then you would need to create a dummy food-list to use as input


#5

ahh i see. this makes now sense!


#6

So where is the mistake?

food = ["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]
return total
print sum(food)


#7

you need to replace food with shopping_list
the sum(food) does work too, because the return will give you already the sum.

hope you have the correct Indentation.

whats your error message?


#8

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

compute_bill(['apple']) resulted in a NameError: global name 'food' is not defined


#9

# Write your code below!
def compute_bill(shopping_list):
    total=0
    for key in shopping_list:
        total += prices[key]
    return total

So hard!!!.. Thank you!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.