a little bit of recursion
Gregor Lingl
glingl at aon.at
Tue Oct 21 17:10:42 EDT 2003
rzed schrieb:
> Dominik Kaspar wrote:
>
>>I'm trying to write a quite big recursive function, but the baseline
>>is the following:
>>
>>def rek(N, L):
>> N = N + 1
>> if N >= 9: return L
>> else: return rek(N, L = L.append(N))
>>
>>print rek(0, [])
>>
>>Why isn't this working?
>>In my opinion it should return [1, 2, ..., 9]
>>
>>Thanks for any answers.
>
>
>
> One problem is that L = L.append(N) does not have the effect you want.
> Try it on its own.
> Another is that it won't return [1,2,...,9] even when that is
> corrected.
That certainly depends on the way one corrects it.
However Dominik first has to notice, that append is a
method, which *changes* L but doesn't return L. Instead
it returns None. So it may be ok to call L.append(N),
but not on the right side of an assignment statement.
You can obsrve what I mean here:
>>> L = [1,2,3]
>>> print L.append(4)
None
>>> L
[1, 2, 3, 4]
>>>
HTH, Gregor
>
>
>
>
More information about the Python-list
mailing list