Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111748
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: learning python. learning defining functions . need help |
| Date | 2016-07-22 09:13 -0400 |
| Organization | IISS Elusive Unicorn |
| Message-ID | <mailman.47.1469193216.22221.python-list@python.org> (permalink) |
| References | <432aeb03-ea14-4ece-8e5b-a7521cf12b84@googlegroups.com> <ji44pbpbaj6olkqcq832ivm7q7gl5d1aca@4ax.com> |
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
<snip>
>---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/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
learning python. learning defining functions . need help sigmaphine1914@gmail.com - 2016-07-21 19:27 -0700
Re: learning python. learning defining functions . need help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-07-22 09:13 -0400
Re: learning python. learning defining functions . need help Chris Angelico <rosuav@gmail.com> - 2016-07-22 23:24 +1000
Re: learning python. learning defining functions . need help justin walters <walters.justin01@gmail.com> - 2016-07-22 08:21 -0700
Re: learning python. learning defining functions . need help Steven D'Aprano <steve@pearwood.info> - 2016-07-23 11:44 +1000
Re: learning python. learning defining functions . need help Random832 <random832@fastmail.com> - 2016-07-22 11:35 -0400
Re: learning python. learning defining functions . need help Chris Angelico <rosuav@gmail.com> - 2016-07-23 01:35 +1000
Re: learning python. learning defining functions . need help "D'Arcy J.M. Cain" <darcy@Vex.Net> - 2016-07-22 11:41 -0400
Re: learning python. learning defining functions . need help MRAB <python@mrabarnett.plus.com> - 2016-07-22 19:36 +0100
Re: learning python. learning defining functions . need help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-07-22 20:04 -0400
Re: learning python. learning defining functions . need help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-07-23 11:39 -0400
csiph-web