Making a Purchase


#1



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


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


I have been stuck on this for two days. I try to solve the problem myself, hoping to learn from it, but i'm just not getting this one. I dont know what i'm doing wrong.


Replace this line with your 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:
        total += food
    return total

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


#2

We need to use item (which is nothing but a fruit name, string) inside of food object and use it as key for global variable prices, and add value of corresponding fruit's price to total.

We need to make changes there..

instead of total += food

use this template..

total += <access the fruit price value by passing food's item as key for prices dictionary>

Reading theory and instruction again might help too!


#3

it wil be work ....

total += food[item]


#4

In the for looping, you are not adding up the prices of the argument 'food'. How can sum up all the 'food' , when it is a list with strings?
You need to replace the 'food' in the looping context by 'prices[item]', as you need to add the prices of the food you chose.


#5

Thank you so much for helping it make sense.


#6

thank you for the help


#7

Thank you for explaining it.


#8

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