Making a Purchase error adding integers


#1



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

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


I know the error means they are trying to add words instead of numbers (strings instead of integers), but I can't figure out how to make it add just integers??

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(prices):
    total = 0
    for item in prices:
        total = total + item
    return total


#2

Hello @designace61973 ,

if you take a look at instruction, it said to put parameter food then loop over food, then add price to total by accessing global variable prices at every key.


#3

@greentreemee
I replaced prices in my code with food, then replaced items with prices, but got the same error. Did I misunderstand your directions or is there something else wrong?

Code now:

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

#4

prices is a dictionary so we can pass key (item) to access the price for any fruit.

prices[fruit_name] will give price of a fruit.


#5

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