learning python. learning defining functions . need help

sigmaphine1914 at gmail.com sigmaphine1914 at gmail.com
Thu Jul 21 22:27:38 EDT 2016


having a little trouble with defining functions. i have a doc called ch5.py and when i was trying the following i had issues 

"
Try It Out: Defining a Function
Try saving the following in your file for Chapter 5, ch5.py.def in_fridge():


       try:
           count = fridge[wanted_food]
       except KeyError:
           count = 0
       return count
How It Works

When you invoke ch5.py (press F5 while in Code Editor) with just the in_fridge function defined, you won't see any output. However, the function will be defined, and it can be invoked from the interactive Python session that you've created.

To take advantage of the in_fridge function, though, you have to ensure that there is a dictionary called fridge with food names in it. In addition, you have to have a string in the name wanted_food. This string is how you can ask, using in_fridge, whether that food is available. Therefore, from the interactive session, you can do this to use the function:


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

---this is what I have and tried to run--
def in_fridge():

    fridge = {'apples':10, 'oranges':3, 'milk':2}
wanted_food = 'apples'
in_fridge()


where am i going wrong



More information about the Python-list mailing list