Https://discuss.codecademy.com/t/making-a-purchase/77272


#1


I've no clue how the code is "behaving". I've tried "key" in place of "item", below.
I've tried ""isolating"" "total" with an intermediate variable....

This is the error message which I get the most:
'Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2
also something like, "


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


#2

I would recommend looking at what list you are supposed to use in the directions and what list the for loop is using.


#3

def compute_bill(food):

you took a parameter named food but you didn't use it in your function.then what's the reason to take?

from instruction

Note that your function should work for any food list

so if we pass compute_bill(['apple']) as an argument simply it'll return 7.5 instead of 2 .Infact if we pass compute_bill('antything') as an argument it'll return 7.5.

make sense :slight_smile:


#4

Thanks for the replies. I need to stop the charge through the lessons and find somewhere to practice
how to think about this kind of programming. I come from Commodore Basic.:neutral_face::neutral_face:


#5

Thanks for the reply. I need to slow down and practice more, I think.


#6

you don't need to go anywhere to practice how to think because it's not something like that.Practice makes perfect.so give time to it as much as you can.
First understand the error message what it wants from you and why aren't you getting that?
believe me,if you can learn to solve the problem going through the error message noone can stop you.
also you have to understand every lines of code.don't just write it :slight_smile:

if you don't understand the error message then ask here :slight_smile:


#7

you were close actually!

shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

alright you made the function 'compute_bill' and it's good but it has 1 problem.

you use the argument 'food' but you didn't use it.

def compute_bill(food):
total = 0
for item in food:

here ^ you wrote 'for item in shopping list' but you have to use the argument 'food'

    total += prices[item]
return total

print (compute_bill(shopping_list))

^ and here i wrote 'compute_bill(shopping_list)'

the argument 'food' = 'shopping_list' hopefully this helps :smiley:


#8

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