ERROR IN - A Day at the Supermarket Making a Purchase


#1



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

IM GETTING THIS ERROR: Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +: 'int' and 'str'


The code runs with no errors and the error message has no sense


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

print("TOTAL PRICES: " + str(compute_bill(prices)))
print("TOTAL STOCK: " + str(compute_bill(stock)))


#2

Not quite...Codecademy is asking you to add the price of the food from the price list.


#3

This are the instructions:

Define a function computebill that takes one argument food as input._
In the function, create a variable total with an initial value of zero.
For each item in the food list, add the price of that item to total.
Finally, return the total.
Ignore whether or not the item you're billing for is in stock.

Note that your function should work for any food list.

Atually seems like the python compiler has an issue, even if I delete all the code and run the program it shows the same message: Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +: 'int' and 'str'. This is the code I did for the excersice;

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

I am not calling the function and the same error displays :-/


#4

You misunderstood what I said. The part of the code I pointed out is wrong. The instructions say:

For that instruction set, you have:

The problem arises because you are using food as a list when you are suppose to be using prices(because you want the price of the food from the price list). So, it technically should be:

total += prices[item]

#5

Small typo, it should be:

total += prices[item]

Compute_bill(['apple']) resulted in a TypeError: 'dict' object is not callable
#6

Whoops! Sorry and thank you for catching that! :slight_smile:


#7

I can't tell the difference.
In any case, I need help. Details at:
https://discuss.codecademy.com/t/making-a-purchase/56421/5


#8

Solved this issue has been.


#9

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 items in food:
total += prices[items]
return total
food = ["apple", "banana", "orange"]
print compute_bill (food)

print ("This is the final solution.")


#10

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