12. Stocking Out - Validator appears to be working incorrectly

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

When I click [Save & Submit Code], the validator gives me an error message. I checked my code many times and finally started writing in print lines to see what my code was doing at certain steps. The last one displays the value the lesson was trying to get me to. The error message from the validator gives erroneous numbers.

Error message example:
Oops, try again. calling compute_bill with a list containing 2 pears, 1 orange and 8 bananas resulted in 39.5 instead of the correct 31.5

These are my results:
banana
6
4
orange
32
1.5
apple
0
2
5.5

The last number is the final, and correct, total: 5.5. Every time I click on the [Save & Submit Code] it gives me different numbers. For example:

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear, 1 orange and 9 bananas resulted in 40.5 instead of the correct 28.5

Oops, try again. calling compute_bill with a list containing 2 apples, 3 pears, 3 oranges and 9 bananas resulted in 49.5 instead of the correct 37.5

Oops, try again. calling compute_bill with a list containing 2 apples, 2 pears, 1 orange and 9 bananas resulted in 43.5 instead of the correct 31.5

Between all of the attempts at saving the code that generated each of the messages above, I made no changes to the code and my results remain the same.

```python

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

compute_bill(shopping_list)

<do not remove the three backticks above>

I skipped so I could move on and the next page was the section wrap up. The instructions: "Click Save & Submit Code to finish this course." Did that and no error.

How can I reproduce what you describe?

My guess is that you had an incorrect function by the same name below the one in your post.

you can’t call the function:

compute_bill(shopping_list)

since that changes the stock.

2 Likes

I don’t think that’s it. The submission test checks both the stock and prices.

I wrote this submission test, to the best of my knowledge it’s solid. I’d love to be proven wrong, but I find it unlikely.

without function call:

with function call:

the error message even says not to call the function. If you don’t call the function, it is working

That’s not the error message in the post though.

Yeah. So there’s something that OP isn’t telling us. Like there being a second function by the same name.

1 Like

Good point, i just assumed he would have a shopping list, two dictionary’s and a function. In that case we will have to wait and see

Reason for Edits: Am experimenting with how the user may have gotten the SCT to catch the incorrect result before catching the call to compute_bill.

For @javawhiz83831’s code, the SCT is always reporting a value that is 12.0 higher than it should be when there are 9 bananas in the shopping list, which exceeds the 6 in stock. So it appears that an alternate version of compute_bill exists that does not check the stock.

Maybe I’m wrong, but in most cases where the user calls the compute_bill function, the SCT will catch and report that call before it catches and reports an incorrectly computed return value from that function. But with some effort, the user could circumvent that to get the incorrectly written function noticed first, for example, by restoring the stock after the function call …

shopping_list = ["banana", "orange", "apple"]

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 += prices[item]
        stock[item] -= 1    
    return total
    
print compute_bill(shopping_list)

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

SCT Message …

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7
1 Like

None of my errors included do not call the function. The errors I pasted is exactly what it gave me. There was nothing in the instructions about not calling the function. I added that assuming I needed the call to invoke the function, but it appears my assumption that the function would only run if called is incorrect.

Okay, reviewed the code again trying to check some of the suggestions from previous replies and found something else. I had two versions of the code. The one I pasted to create this thread and another with some minor changes. The errors were created using the second of the two versions. Reconciling the two versions (and a little trial and error) I located the problem in my code: I used --stock[item] instead of stock[item] -= 1. Not sure why -=1 didn’t work the first time I used it (perhaps I had another error in my code I fixed after changing to --).

When I change to -=1 with the call, then I get the error message that includes do not call the function. So what does -- do if it is not the same as -=1? Presumably, if two minuses were invalid code for Python I would get an error for that instead… ?

-- doesn’t exist in python. It does exist in javascript, C and many other languages, but not in python. That does explain the error. Did you manage to pass?

Yes, all set. Just curious, if pre-decrement does not exist, why do I not get a syntax error?

sorry, i should put it differently. It does exist, but not to decrease a value/variable by one. You can use it as plus:

1 -- 1

which is 2. -- is plus, it is just math

I passed this exercise a long time ago but I did not had to use function call. Now calling function gives a warning.

I test my code in laboratory Codecademy labs link it is correct.
So, for an unknown reason it’s showing the warning.

@mashuk ,

Codecademy wants you to wait until the final exercise, which is the next one, to call the compute_bill function. While it does not actually ask you to call it then, you can do so without getting that Oops, try again. … message.

Nope, The final exercise instruction:

Click Save & Submit Code to finish this course.

What’s happening is you cannot/(not good practice) reassign(stock[item] = stock[item] -1) what already in argument in a function call. When you are calling the function with shopping list,

first python enters to the list.
Then it finds that the first index(banana) in the list belongs to a dictionary.
The dictionary called stock. but its not finished yet.

But when it wants to go to the next item in the list it finds number of banana has been reassigned, so it confuses the function call.

same is the case with me. Its showing the same error and giving the incorrect answer

I did it another way (everything is fine) where I did not change the stock dictionary but it says I’m returning none. I sort of understand why it’s saying that (treating function as an object and should be reusable) but I would be interested if someone assure they have done it with the knowledge from the python exercises here in codecademy and pass the exercise with function call.

Is there anyone who have done it with function call and without using any other knowledge but the codecademy exercises?

You can actually call the function. And you know you want to.