On Sun, 18 Dec 2005, Alan Gauld wrote:
> > def adder(**args): > > argsList = args.values() > > sum = argsList[0] > > for x in argsList[1:]: > > sum = sum + x > > return sum > > > line 3, in adder > > sum = argsList[0] > > IndexError: list index out of range > > > > This is caused by the line: print adder(). Obviously if adder() > > doesn't receive any arguments, it can't build the lists resulting in > > an IndexError. What is the best way to solve this? Should I write > > some syntax into the function to check for arguments? > > The Pythonic way is to use exceptions. Hi Alan, I disagree for this particular situation: adder() can be written so it doesn't raise an exception on empty input. Brian gave a hint about this when he asked: what happens if we try to sum up an empty sequence? Without looking at the body of the function, what value do we want to get from the call to "adder()" with no arguments? Once we know this, then we can go toward defining our function to do what we want. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor