a little bit of recursion

Skip Montanaro skip at pobox.com
Tue Oct 21 17:37:44 EDT 2003


    Gregor> Skip Montanaro schrieb:
    Dominik> def rek(N, L):
    Dominik>    N = N + 1
    Dominik>    if N >= 9: return L
    Dominik>    else: return rek(N, L = L.append(N))

    >> In the recursive calls, L is None.  Just pass L as the second arg:
    >> 
    >> def rek(N, L):
    >>     N = N + 1
    >>     if N >= 9:
    >>         return L
    >>     L.append(N)
    >>     return rek(N, L)

    Gregor> Nice, but will not append 9 as desired

Sure, but that's a logic error, not a misunderstanding about what L.append()
returns.  I suspect the latter was what confused Dominik.  Once he runs
something that doesn't raise an AttributeError, he'll quickly discover that
his test should be "if N > 9".  He couldn't even get to that point with his
original code.

Skip





More information about the Python-list mailing list