Prev: def method with variable no of parameters file.writeStuff(n, a1, a2, ...an)
Next: Irony overload [was Re: off topic but please forgive me me andanswer]
From: monkeys paw on 4 Apr 2010 19:46 Why does the following fail with the Traceback? def add(x,y): return x+y for rrr in range(1,20): reduce(add, range(1, r)) Traceback (most recent call last): File "<interactive input>", line 2, in <module> TypeError: reduce() of empty sequence with no initial value
From: Benjamin Kaplan on 4 Apr 2010 19:55 On Sun, Apr 4, 2010 at 7:46 PM, monkeys paw <monkey(a)joemoney.net> wrote: > Why does the following fail with the Traceback? > > def add(x,y): return x+y > for rrr in range(1,20): > reduce(add, range(1, r)) > > Traceback (most recent call last): > File "<interactive input>", line 2, in <module> > TypeError: reduce() of empty sequence with no initial value > -- >>> for x in range(1,1) : .... print x .... >>> ranges start at the first value and go up to, but not including, the second value. So range(1,1) gives you no values. reduce doesn't know what to do with an empty sequence, so it throws an error. > http://mail.python.org/mailman/listinfo/python-list >
From: Terry Reedy on 4 Apr 2010 20:02
On 4/4/2010 7:46 PM, monkeys paw wrote: > Why does the following fail with the Traceback? > > def add(x,y): return x+y > for rrr in range(1,20): I presume that this was 'for r...' > reduce(add, range(1, r)) and that this was indented. > Traceback (most recent call last): > File "<interactive input>", line 2, in <module> > TypeError: reduce() of empty sequence with no initial value Just what it says. To debug this sort of this, try printing values just before the line that fails. When r is 1, range(1,1) = []. So either start r at 2 or add the third arg to reduce(add, range(1,r), 0). Terry Jan Reedy |