11. Making a Purchase, having a problem


#1

here is the code i wrote:

but it keep saying "Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2"

I've compared other answers and seems like my format is correct.

Could anyone please let me know where is wrong? Thank you so much!


#2

the compute_bill function should calculate the cost of shopping, so you can pass different shopping_lists when calling the function, this shopping list is then stored in your function parameter which is food. So that is what i would loop over, not prices.


#3

Thanks for your explaining, I've updated my codes below, but i still got error message, could you please take a look again? Thank you so much!


#4

can you copy paste your code to the forum so i can run it? Makes debugging a lot easier


#5

oh, sorry, my bad!

here it is:

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

print compute_bill (shopping_list)

#6

this code looks different from the one in the image?


#7

here it is, i just played around a little bit. :slight_smile:

But the thing is, it works right now, i didn't do anything compare with the code in my last post, it's so weird.

Thanks so much for helping me !!!!

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

print compute_bill (shopping_list)


#8

weird, but good you made it work. Could be your browser was sending cached data or something


#9

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