Right answer but error message


#1



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


Input the shopping lest, compare it to the prices, an generate the bill.
I'm getting the correct answer, but this error message :

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


However, the correct result is 7.5 and does output in the console, with no interpreter errors

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

print compute_bill(shopping_list)


#2

but what if a second customer comes to your store and wants to buy an apple:

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

# customer 1
print compute_bill(shopping_list)
# customer 2
print compute_bill(['apple'])

he get charged for banana, orange and apple while only buying an apple


#3

Wouldn't that just use a different shopping list?


#4

no, your function always return the same amount. Did you see the code i posted? Did you run it?


#5

So how do we fix it?


#6

to represent our customers, we use function calls. At function call we supply an argument (the products customers want to buy), this arguments (which are list), get copied into the function parameter, giving us access to each customer product. So, if we then loop over food (your function parameter), we can add the price of each product to total


#7

Show me the code. I don't understand what you want. This is far from the way I would do it and I am just not tracking what you want.


#8

A post was split to a new topic: Compute_bill


#9

look at this code:

# function with parameter food
def compute_bill(food):
    print food

# function calls with arguments 
# customer 1
print compute_bill(shopping_list)
# customer 2
print compute_bill(['apple'])

see the comments in code, i am going to use some of those terms

see how the arguments (list with products customer wants to buy) get copied into the function parameter food? So if we then loop over food, your function works for all lists supplied as argument, not just shopping_list (a list you defined on line 1)


#10

Hi @johnmoscato,

There are few problems with your function you created ,I will point them.

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

I will go explaining line by line.

1.You created a function called compute_bill()
2.You pass in a parameter called food , This food parameter is a list here .
It can be any list(with the context of the code,It can be list of fruits,any list)

Remember the parameter inside a function are local variables and they're replcaed by the value when you call your function and pass them.

Two concepts to remember.

Global variable - the variable defined outside of function is called global variables and they can be accessed any part of the program.

Local variable -- The variables defined inside the function are used to be in local scope and they exist till you call your function and pass it some value,all the variables will be replaced by the value you pass. local variables exist till function gets called and vanish when call gets finished.

An example..

def double(x):
    return 2 * x

I defined a function double and passed it a parameter x
Now lets say I defined a variable outside of function..

z = 3 #global variable

When I call the function like this..
double(z)

The parameter x is replaced by the value of z and calculation is done on z
and it return 6.

I hope it clears your doubt.

3.You start looping over shopping_list using iteration variable food?
Two mistakes here..

A.You already used food variable as your function's parameter so you need to use some other iteration variable other than food. (EDIT : It will create confusion between which is function's parameter or which is iteration variable.)
B.You do not have to iterate over just shopping_list list, You have to iterate over your general list,your parameter food here.

4.total=total+prices[food]
You have to use this..
total=total+prices[iteration_variable]

5.last line is okay.

Make changes as I mentioned and tell me what you get!


#11

This answer makes no sense and returns interpreter errors (multiple)

I don't understand why you would hard code the shopping list anyway - you would have to reach into the code to change it. Why don't you just have an input to build the list interactively? That way you would always have an updated and customized list


#12

Can we see what code you have now?


#13

Wonderful. That's not what the instructions tell you to do. We'll give it a try.


#14

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