A day at the Supermarket 11. Making a purchase


#1

Hello Ladies and Gents hope you all are having a wonderful day! I am having an error with 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
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
            item += total
            return total

It gives the following error:Oops, try again. compute_bill(['apple']) resulted in a TypeError: cannot concatenate 'str' and 'int' objects


#2

The issue is here:

Now, we need to return for each item in the food list, and then add the price of that item to total. Your return indentation seems off too.

        total += prices[item]
    return total

At the end of the day, you have it as:

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

#3

Thank you! Have a Great Day!


#4

And you too.

Have fun coding! :slight_smile:


#5

I am still having problems i rewrote the code;

["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:
     food += total

error code:Oops, try again. compute_bill(['apple']) resulted in a TypeError: 'int' object is not iterable


#6

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


#7

After the
"For item in food:
total += price[item]"
return total

not "food += total". That's where you go wrong buddy.. I was stuck for a bit too before I figured it out


#8

thx i will try to fix it


#9

Maybe a hint or clue could be useful


#10

Hi There,

I have the following code, which I think is correct. However, I am still getting an Oops; "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7". Any ideas of what may be going on? 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 food:
total += prices[item]
return total


#11

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