Stocking out


#1



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


my code behaves correctly but it gives me this error code
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 30 instead of the correct 7


i expect no error messages


#shopping_list = ["banana", "orange", "apple"];
shopping_list = {"apple" : 1, "pear":1, "orange" : 0, "banana": 1};
#shopping_list = {"banana": 1, "orange" : 1, "pear" : 1};
#shopping_list = {"banana": 9, "orange" : 3, "apple" : 0, "pear" : 0};

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):
    pTotal = 0
    total = 0
    for kFood in food:
        if stock[kFood] > 0:
            if shopping_list[kFood] >= stock[kFood]:
                
                pTotal = stock[kFood] * prices[kFood];
                #stock[kFood] = 0;
            else:
                pTotal = shopping_list[kFood] * prices[kFood];
                #stock[kFood] = stock[kFood] -shopping_list[kFood]; 
            total = total + pTotal;
    return total
print compute_bill(shopping_list);


#2

@cvquach,

Have a close look at

shopping_list[kFood]


#3

Thanks leonhard,

I fixed it but i get a new error involving string is indices, but i am using it as a hash/key for my array so i dunno why its throwing that error

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: list indices must be integers, not str
`
shopping_list = {"apple" : 0, "pear":1, "orange" : 1, "banana": 1};
shopping_list_1 = {"apple" : 2, "pear":2, "orange" : 2, "banana": 8};
shopping_list_2 = {"apple" : 1, "pear":2, "orange" : 0, "banana": 7};
shopping_list_3 = {"apple" : 1, "pear":1, "orange" : 0, "banana": 1};

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):
pTotal = 0
total = 0
for kFood in food:
if stock[kFood] > 0:
if food[kFood] >= stock[kFood]:

            pTotal = stock[kFood] * prices[kFood];
            #stock[kFood] = 0;
        else:
            pTotal = food[kFood] * prices[kFood];
            #stock[kFood] = stock[kFood] -food[kFood]; 
        total = total + pTotal;
return total

print compute_bill(shopping_list);
print compute_bill(shopping_list_1);
print compute_bill(shopping_list_2);
print compute_bill(shopping_list_3);`


#4

@cvquach,

The Oops-message, generated by the code-checker....

Oops, try again.
calling compute_bill with a list containing 1 apple, 1 pear and 1 banana
caused the following error: list indices must be integers, not str

points to the fact
that you will have to create a function that will get a list as its argument
and NOT as you do,
creating a function which takes a dictionary as its argument

So re-write your function,
so it will process a list ["apple","pear","banana"]
http://stackoverflow.com/questions/1514553/how-to-declare-an-array-in-python

+++ function talk

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )