Newbie question...

jhorn94 at my-deja.com jhorn94 at my-deja.com
Wed Sep 27 09:20:36 EDT 2000


I'm going thru the Learning Python book and am stumpped.  The exercise
calls for a function that accepts and arbitrary number of keyword
arguments, then returns the sum of the values.  I can step thru the
arguments with a for statement, but I can't figure out how to accumlate
a sum of the values.  Any help is appreciated.

def adder(**args):

    for x in args.keys():
        y=y+x
    return y

print adder(good=1, bad=2, ugly=3)
print adder(good="a", bad="b", ugly="c")


Traceback (most recent call last):
  File "C:\Python16\Pythonwin\pywin\framework\scriptutils.py", line
301, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Python16\examples\lpython\sol\CH4EX4.PY", line 8, in ?
    print adder(good=1, bad=2, ugly=3)
  File "C:\Python16\examples\lpython\sol\CH4EX4.PY", line 4, in adder
    y=y+x
UnboundLocalError: y


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list