Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: learning python. learning defining functions . need help Date: Fri, 22 Jul 2016 09:13:23 -0400 Organization: IISS Elusive Unicorn Lines: 128 Message-ID: References: <432aeb03-ea14-4ece-8e5b-a7521cf12b84@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Tv6XuLHqVnkoKeIjISmprQOfzeea2dECdueS+NgRofKA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'assignment': 0.07; 'subject:help': 0.07; 'drinks': 0.09; 'exits': 0.09; 'garbage': 0.09; 'globals': 0.09; 'implies': 0.09; 'item,': 0.09; 'message- id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '(at': 0.13; 'def': 0.13; 'argument': 0.15; 'thu,': 0.15; 'value.': 0.15; '2016': 0.16; '30)': 0.16; 'add(self,': 0.16; 'avail': 0.16; 'dictionary,': 0.16; 'globals.': 0.16; 'item):': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'remove(self,': 0.16; 'subject:python.': 0.16; 'written.': 0.16; 'found,': 0.18; 'module,': 0.18; 'subject:need': 0.18; 'try:': 0.18; 'url:home': 0.18; 'keyerror:': 0.22; 'defined': 0.23; 'chapter': 0.23; 'tried': 0.24; 'module': 0.25; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'defining': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'dictionary': 0.29; 'subject:learning': 0.29; 'print': 0.30; 'code': 0.30; 'normally': 0.30; 'skip:_ 10': 0.32; 'point': 0.33; 'class': 0.33; '-0700': 0.33; 'file': 0.34; 'except': 0.34; 'skip:d 20': 0.34; 'skip:c 30': 0.35; 'trouble': 0.35; 'level': 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'basic': 0.36; 'skip:{ 10': 0.36; 'stock': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'beyond': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'wrong': 0.38; 'skip:p 20': 0.38; "didn't": 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'term': 0.60; 'your': 0.60; 'email addr:gmail.com': 0.62; '30,': 0.63; 'granted': 0.63; '20,': 0.66; 'subject:. ': 0.67; 'saving': 0.70; '26,': 0.72; 'jul': 0.72; '>def': 0.84; 'dict()': 0.84; 'fridge': 0.84; 'now...': 0.84; 'subject: . ': 0.84; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-68-179-16.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <432aeb03-ea14-4ece-8e5b-a7521cf12b84@googlegroups.com> Xref: csiph.com comp.lang.python:111748 On Thu, 21 Jul 2016 19:27:38 -0700 (PDT), sigmaphine1914@gmail.com declaimed the following: > >" >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 >---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 Off-hand -- you didn't follow the instructions. Your "in_fridge" function merely attaches the local name "fridge" to a dictionary, then exits (at which point the dictionary and local name are garbage collected). The assignment implies that both the dictionary "fridge" and "wanted_food" are module (file) level globals. Module globals can be seen and read by functions defined in the module, but normally not written. The code you failed to include uses both globals to look up the availability of the item and, if found, returns the count value. Granted -- from where I sit, that whole use of globals for both the dictionary and the search term smells like 1970s K&K BASIC using GOSUB. At the least, the search term should have been passed as an argument in the function call print (in_fridge("apples")) for example.... Now... Going much beyond the assignment (if you were having trouble with the assignment, this will seem like magic) [Python 2.7]: -=-=-=-=- """ Refrigerator Stocking Example July 22 2016 Dennis L Bieber """ class Refrigerator(object): def __init__(self, stock=None): if stock is None or type(stock) != type(dict()): self._stock = dict() else: self._stock = stock def available(self, item): return self._stock.get(item, 0) def add(self, item, amount): self._stock[item] = self.available(item) + amount return self.available(item) def remove(self, item, amount): avail = min(self.available(item), amount) self._stock[item] = self.available(item) - avail return avail if __name__ == "__main__": drinks = Refrigerator({"coca cola" : 20, "pepsi cola" : 10, "rc cola" : 5, "vernors" : 31}) fruits = Refrigerator() fruits.add("apples", 30) fruits.add("pears", 25) fruits.add("kiwis", 10) print "vernors", drinks.available("vernors") print "rc cola", drinks.available("rc cola") print "take 10 rc cola", drinks.remove("rc cola", 10) print "rc cola left", drinks.available("rc cola") print "take 5 vernors", drinks.remove("vernors", 5) print "vernors left", drinks.available("vernors") print "take 1 kiwis", fruits.remove("kiwis", 1) print "kiwis left", fruits.available("kiwis") print drinks._stock #going inside the class print fruits._stock -=-=-=-=- C:\Users\Wulfraed\Documents\Python Progs>refrigerator.py vernors 31 rc cola 5 take 10 rc cola 5 rc cola left 0 take 5 vernors 5 vernors left 26 take 1 kiwis 1 kiwis left 9 {'coca cola': 20, 'pepsi cola': 10, 'vernors': 26, 'rc cola': 0} {'kiwis': 9, 'apples': 30, 'pears': 25} C:\Users\Wulfraed\Documents\Python Progs> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/