Newbie question...

Maurizio Turatti mturatti at yahoo.com
Wed Sep 27 11:07:33 EDT 2000


I don't know if this fit your question:


def adder(*args):
	y=args[0]
	for x in args[1:]:
		y=y+x
	return y

print adder(1,2,3)
print adder('a','b','c')


jhorn94 at my-deja.com wrote:
> 
> 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