Weird output Stocking Out


I know the code is not complete yet, but i dont understand the output.
If u run the same code in an IDE it gives me complete different output.
Am i running into a bug?

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


calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7
!! Where does the pear come from?

Console output:
why the “7”?

I’m thoroughly confused
IDE output:


IDE’s don’t run code. They’re a layer between you and various tools, python being one such tool. Python runs your code.

The 7 is likely something your function printed when processing the test case that your error message mentions. Codecademy has a bad tendency of printing some things it shouldn’t.


I said when the IDE runs the code, i meant when i press “run code” in pycharm.

Sometimes its hard in codeacademy to know when the code is good or not when it gives bogus output. AFAIK using print statements is quite normal to check code… right?

Sometimes error messages do not make sense. there was no way “pear” could get included in the evaluation…
So as i have the code running properly outside codeacademy i eventualy pressed “get code”.
The proper code as codeacademy returned did not even call the function with compute_bill(shopping_list).
How then does the function know what dict to use?

Is it just me. or is codeacademy quite confusing at times.


As a means of getting information out of the program onto the screen… yes that’s what print does, so yes, for that purpose, quite normal

But you’ll for example notice that while doing 1 + 1, the + operator doesn’t print 2, or anything else

Your function should handle lists containing fruits, pear being one of them. Testing your function by sending it various lists of fruits is entirely reasonable.

If the function isn’t called then it doesn’t run at all. However, the task wasn’t to run it, it was to create the function, so for that purpose there’s no need to call the function. You might however call it in order to test it, and print out the results of that.


Note that it’s abnormal (as with the + operator not printing the result) for compute_bill to print anything (unless you’re trying to figure out what’s happening inside it (debugging)

What you’d do instead is:

result = compute_bill(['apple'] * 5)
print 'calling compute_bill with a list containing 5 apples resulted in: %r' % (result,)


That is a good point, the task was to create the function, no where it was asked to call the function.
Pear was indeed part of the dictionary, however pear was not part of the list of items used as arguments for the function. Therefor i was confused why it came up in the error message.
But then again as i realise, i don’t not know the code that is used to validate/check the code we mortals write.
Sometimes im just a bit confused, and not sure if its me or codeacademy quirks, especialy when i am not that confident in my code…
Anywayz…back to coding & learning… thank you for your explanations .


Ideally you should test your code yourself, see that the thing you wrote had the desired effect.
This is hard because you will often not read the description well enough, for example presenting the result by printing and producing None where the result should have gone.
It can also be difficult to reason about whether things are correct for all input. Generally you do that by making a convincing argument for why all possible inputs will be handled correctly (yes, this is math territory, the kind of math where you reason about things, which is the fun part)

It’s also a good idea to test it locally (a controlled environment), though again, you might not run it the same way as codecademy does, for example one might use python3 (as you should, really) instead of python2 (which codecademy still uses) but you seem to have already figured that part out.

Write code – observe the effect of said code. Pretty important for learning.


Indeed… i tend to try a lot of things… to see what it does…often results in program failures… but learning things along the way…
Python3. hmmm most code i write will be for Centos/RHEL systems. and last time i checked it was kinda restrictive on the repo that contained python3.
That said. i have not noticed the limitations/issues on python2… i would assume i’d run into challenges not using python3 down my coding path at a certain point.
At this point i have been kinda reluctant to try Python3


You’ll be stuck in 2010, that’s all.
You don’t necessarily need to use the same python installation that the operating system uses, but there may be political reasons for doing so.


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