TypeError, I know why but not how!?

alex23 wuwei23 at gmail.com
Thu Jul 10 22:04:35 EDT 2008


On Jul 10, 11:15 pm, ssecorp <circularf... at gmail.com> wrote:
> I don't fully understand why I have to do this.

Because your function is recursive.

You call your function passing it a list of numbers. It generates a
new number and looks to see if it's in the 'placed' list. If it is, it
calls itself again, but if it isn't, it returns the number & an
updated 'placed'.

Without placing a return before the recursive call to validate, it
calls validate in place, ignores its return result, then drops out of
the conditional to the end of the function, where an implicit 'return
None' occurs.

By returning what validate passes back, you in effect pass it back up
the chain of recursive calls, so the final return will _always_ pass
back the number, list combo you require.

- alex23



More information about the Python-list mailing list