0

Whenever I enter the following code:

def in_fridge():
    try:
        count = fridge[wanted_food]
    except KeyError:
        count = 0
    return count

fridge = {"apples": 10, "oranges": 3, "milk": 9}
wanted_food = "apples"
in_fridge()

Into the IDLE, "10" is outputted.

When I enter the same code into the code editor and then press F5, nothing is outputted. As a test, I created a new file in the code editor, entered:

print ("Hello World") 

and dutifully got the outputted result, i.e. hello world displayed in a new window from the IDLE shell.

So I am curious as to why I get a value displayed in the IDLE environment but not the code editor, when I have entered precisely the same code.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
apronedsamurai
  • 73
  • 1
  • 11
  • 3
    You don't have any `print` in your first code snippet. –  Dec 05 '16 at 12:49
  • This question is actually not about IDLE. The output displayed is the output generated by the python interpreter that runs the code. The difference in behavior is the difference one sees running the same code directly in Python in interactive versus batch mode in the console. IDLE just makes the comparison slightly easier by running code directly from the editor. – Terry Jan Reedy Dec 06 '16 at 22:12

4 Answers4

6

You've called in_fridge but you haven't done anything with the result. You could print it, for example:

result = in_fridge()
print(result)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
4

You have to print that, because in IDLE the return is shown on console if not stored in a variable. Which doesn't happen when running a script, in script if something is returned by a function you need to capture that. Using = operator like result_of_func = function_name() and then print the value storred in that variable print(result_of_func)

This will work :

def in_fridge():
    try:
        count =fridge [wanted_food]
    except KeyError:
        count =0
    return count

fridge ={"apples":10, "oranges":3, "milk":9}
wanted_food="apples"
print (in_fridge())

Or :

in_fridge_count = in_fridge()
print ('Count in fridge is : ',in_fridge_count)
harshil9968
  • 3,254
  • 1
  • 16
  • 26
  • So if in IDLE the return is automatically printed, does that mean conversely that in code editor, the return is NOT automatically printed? – apronedsamurai Dec 05 '16 at 12:53
  • 1
    Yes what ever be the return in IDLE it's outputted to console if not captured into any variable. Which is not the case in running via script. Read the updated answer. – harshil9968 Dec 05 '16 at 12:54
  • kindly accept the answer if this is clear to you now. – harshil9968 Dec 05 '16 at 12:58
2

You are not priting the result of the in_fridge call, you should print it:

def in_fridge():
    try:
        count =fridge [wanted_food]
    except KeyError:
        count =0
    return count

fridge ={"apples":10, "oranges":3, "milk":9}
wanted_food="apples"
print(in_fridge())
Pierre Barre
  • 2,174
  • 1
  • 11
  • 23
1

In order to display the output, you need to print it :

print(in_fridge())
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44