12. Stocking out. Why happens this error to me?


#1

This is my code window.

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:
if stock[item] > 0 :
total += prices[item]
stock[item] -= 1
return total

compute_bill(shopping_list)


and, it occured error message:

"Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}"

"
But, i can't understand about this message.


#2

I solved it. It leaves me cold............
I just deleted "compute_bill(shopping_list" line.

If you write code like me, you should delete compute_bill function line.


#3

Can somebody explain why the "compute_bill(shopping_list)" line has to be removed? I don't get it.


#4

i don't understand too((


#5

I didn't write enough explanation why compute_bill function is deleted. Sorry about that.

According to console's error message, make sure to not call compute_bill function since it changes the stock. It means that you just define compute_bill function but not call this.
Because correct answer or compile result is unchanged stock dictionary and arranged compute_bill( ).

If you still cannot understand, please reply me.

Thx :smile:


#6

i don't understand, why it does not work..

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 key in prices:
if stock[key] > 0:
total += prices[key]
stock[key] -= 1
return total

print compute_bill(shopping_list)

if i commenting string with call of "compute_bill(food)", system say "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 8.5 instead of the correct 7"
if i use calling of function, i see that error: "Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}"
what i do wrong?


#7

Ok. Just remove only compute_bill ( shopping_list).

I mean, just remove call function line.
And please read error message carefully.

Answer is in error message.


#8

i tried to do that, but i see error:
"Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 8.5 instead of the correct 7"
and i don't understand, why my function make result with error... it must be 7, but i have 8,5. I don't understand, why it use "apple", if it's stock is 0((
def compute_bill(food):
total = 0
for key in prices:
if stock[key] > 0:
total += prices[key]
print total
stock[key] -= 1
return total

that's ny error:
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 8.5 instead of the correct 7


#9

YYYYAAAAHHHHHOOOOOOO!!!! i done this %*%^#@!!!!
i change "prices" in my loop to "food". and now my loop locking for:
"for key in food"
and now it work!!! i happy))) i killing 3 days for this lesson!


#10

In your codes, please watch "for loop" carefully.

According to your codes, first "key" variable start from "banana" to "pear" in "prices" dictionary not "shopping_list" since "for loop" syntax is

for variable in list( or dictionary)__name.

So, please change "for key in prices:" line ==> "for key in food" ( maybe it's default setting).


#11

CONGRATULATION~~~ YYYYYYYYAAAAAHHHHHHHHH!!!!!!!!

^_______________^ I'm happy to solve problem with you.

You grow up quickly.:smile:


#12

Thank you :+1:
but i don't understand, why i doesn't do that quikly... mabe i so foolish((


#13

def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total


#14

I cannot understand this. My question goes beyond "Why do you have to delete the "print compute_bill(shopping_list)" line. My question is how can you delete it. Wasn't the point of the algorithm to print the cost of the groceries? Without this line, how does it get done?


#15

Yes. As you said, it's not the point of print about costs of groceries. I think.. Python interpreter what we cannot see check our codes in console.

So.. We just define function correctly, and dont print result.
I just apply comments of console's error message.