11. Making a Purchase


#1

My current 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 computer_bill(food):
total = 0
for i in food:
total = total + prices[1]
return total

i get error messages saying - Oops, try again. Did you define a function called compute_bill?
and I do not see my mistake?


#2

Your function is called compute R_ bill (without the spaces) and not compute_bill :grin:


#3

Ahh thank you that was a Silly mistake


#4

Hi,

below is my code
def compute_bill(food):
total=0
for item in food:
total += prices[items]
return total

however, i am getting an error: File "python", line 22
SyntaxError: 'return' outside function

Please help


#5

try total = total + prices[1]

that worked for me


#6

Hi, can someone check my code. I can't find what's wrong.

This is the error I get:

"Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2"

This is my code (I included my troubleshooting attempt commented out):

food_list = ["banana", "apple", "orange", "pear"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
"""total = 0
for item in food_list:
print item
print prices[item]
total += prices[item]
print total"""

Write your code below!

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


#7

You need to indent the return line, so that its within the function.


#8

Ok, I figured it out. Just needed to change the following code:

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


#9

Thanks...it is working now :slight_smile:


#10
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 shopping_list:
        #if item == food:
            total = total+prices[item]
    return total

I keep getting this error "
Oops, try again.
compute_bill(['apple']) returned 7.5 instead of 2"

To fix this when i add if it gives me error " Oops, try again.
compute_bill(['apple']) returned 0 instead of 2"

I did print of total which actually prints 2 in console


#11

check your 2nd to last line of code.

It should look something like this: total = total + prices[1] :wink:


#12

Hello, I need clarification on how 'arguments' are input into functions.

In this lesson, per @vikas.jain above, the code is:

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 shopping_list:
#if item == food:
total = total+prices[item]
return total

However I do not understand where 'food' is defined?


#13

This worked for me...

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

#14

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 x in food:
        total += x
    return total

Above code gives the error:
compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'

In some way my code is calling the key rather than the value from the dictionary?


#15

Yes that does work.
Is it that
prices[item]
returns the value for each key rather than the key (which I think was my problem below)
Thank you!


#16

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

Guys Python is very sensitive to indentation Last time I didn't follow the indentation for Last return total and got error n finally after weeks i noticed and Happy to move forward

!!!Be Careful of Indentation!!!
Check it yourself if you indent further 4 space last line then you will get error message


#17

yep, me too, don't get how code understand this argument, and searches it in prices...


#18

food is an argument. you gonna pass through the function compute bill its just an place holder.
we would feed prices list through compute bill function:

Correct me if I am wrong???


#19

Just curious how does the += work in this function?


#20

Here is my code:
shopping_list = ["banana", "orange", "apple", "pear"]

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

print compute_bill(shopping_list)

Unfortunately it is printing 4 when it should be printing 10.5? I do not understand why as it should be adding the price of each item as it finds it in the prices list.