Making a Purchase


#1

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

I don't understand why I am geting this error.

Here is the 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 = total + item
return total

#2

Hi. Remember that it's asking for the price of the item. Your code should like these:

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


#3

When I run this code, it comes back with the wrong answer for apples. my code looks like this:

python

Write your code below!

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

python

my error message is:None
Oops, try again. compute_bill(['apple']) returned 4 instead of 2


#4

when i entered the code as you mentioned, it returns with error = "Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5"
Any ideas?


#5

Hey. Can you send me the full code so that I can take a better look at it? Thanks. Here you have mine. Are you at lesson 11. Making a purchase?

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


#6

If you got this error: {Oops, try again. compute_bill(['apple']) returned 4 instead of 2}
do this:

def compute_bill(food):
    total=0
    for item in food:
        total += prices[item]
    return total  -->  This must be aligned with the def indentation

#7

thankx a lot it's working , but may i ask why ? why when we aligned return with def it works


#8

First, food is a list, so you cannot write "for food in shopping_list". Here, the list "food" is your so-called "shopping_list".
So you should write:" for item in food:"

Second, total += prices[item]


#9

Hi,
at first I wrote

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

can you explain me why it didn't work?


#11

Because the return command orders the function to be "computable" and then it has to be indented with the function and not with other commands.
Here you can find a good explanation of the return command:
https://www.codecademy.com/en/forum_questions/51c0e35d7c82caace80008b1


#12

Here's what i see:
When you write for item in food:
you are telling the program to do this:
for every item available in the food (list, dictionary etc.)... do this (xxxxxx), and then you are not talking to it in terms of [item] anymore but in terms of [key], you should seek to speak to the machine like a kid: do not mix the topics while explaining something to it.


#13

hahah thank you good sir


#15