[Tutor] recursion surprise
Dave Angel
davea at davea.name
Sun Jun 9 01:46:38 CEST 2013
On 06/08/2013 07:12 PM, Jim Mooney wrote:
> On 8 June 2013 15:43, Dave Angel <davea at davea.name> wrote:
>> On 06/08/2013 05:46 PM, Jim Mooney wrote:
>
>> Post the same code that you ran, to get that output. Otherwise you're
>> wasting our time.
>
> Okay, here it is not working a different way, copied exactly, but it
> looks like I do have a return statement, and that it should be
> returning 11, not None. I must be missing something in my
> understanding:
>
> def addone(num):
> if num > 10:
> return num
> num = addone(num + 1)
>
> print(addone(1))
>
> result:
>>>>
> None
>>>>
>
>
Did you even read my message? Or Mark's? Or look at the code I posted?
You are missing a return statement at the end of the function, so
after the assignment num=addone(num+1) it will return None, by definition.
--
DaveA
More information about the Tutor
mailing list