'Function in Python not producing output?

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.



Solution 1:[1]

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)

Solution 2:[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())

Solution 3:[3]

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

print(in_fridge())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Roseman
Solution 2 Pierre Barre
Solution 3 Taufiq Rahman