Making a purchase


#1


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

Error message: Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2

Please can you provide guidance as to why my code is not working.

Thanks


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


#2

Your function takes an argument but doesn't use it.


#3

Many thanks. Please could you show me how it should be done. I am struggling with this one.


#4

Just consider how you'd do it manually with what information is available in that function, then translate that to code. You have to start by having an intention for what you want to do.


#5

Many thanks for your help so far. I have thought through it but am now getting this error:

Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2

Here is 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 x in shopping_list:
total = total + prices[x]
print (total)
return (total)

print (compute_bill(shopping_list))

Please can I ask for your guidance again.

Kind regards

Paul


#6

I'd take a look at line
19
and
20


#7

Don't put list directly in function.


#8

right answer

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


#10

Here is my code try this.
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 item in food:
              total = total + prices[item]
     return total #<== Indent so that (return total) is on the same line as (total = 0])

print compute_bill(shopping_list)

#11

Hi,all
Just joined (2 days).I am a retired public health professional.Learning to code out of sheer interest as a hobby.This is my first post.
I am having the same issue(section: Making the purchase). But adding the prices manually (banana=6,orange=1.5,apple=2) it comes to 7.5(4+1.5+2).But error message say that it should be 2.
Unable to understand this.

my code is below:

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

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

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


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

compute_bill(shopping_list)

#12

Oops........ forgot to sign........here goes
Hi,all
Just joined (2 days).I am a retired public health professional.Learning to code out of sheer interest as a hobby.This is my first post.
I am having the same issue(section: Making the purchase). But adding the prices manually (banana=6,orange=1.5,apple=2) it comes to 7.5(4+1.5+2).But error message say that it should be 2.
Unable to understand this.

my code is below:

indent preformatted text by 4 spaces
shopping_list = ["banana", "orange", "apple"]

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

}

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

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

compute_bill(shopping_list)

thanks & cheers

PKJ


#13

Don't put your list into your function. Use your argument so when you apply a list to your argument later you can use the function for things other than that singular list.

def compute_bill(food_list):
total=0
for i in food_list:
total +=prices[i]
return total

compute_bill(shopping_list)


#14

https://discuss.codecademy.com/faq1

Don't share code please. Against Code-academy rules


#15

https://discuss.codecademy.com/faq1

Read, don't share code please.


#16

Ok, many thanks all. I have managed to figure out where I was going wrong with all your help.


#17
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(shopping_list):
total = 0
for food in shopping_list:
    total+=prices[food]
return total

its work


#18

Thanks. Your answer helped me understand how it works.