[Tutor] recursion surprise

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Jun 9 00:02:24 CEST 2013


On 08/06/2013 22:46, Jim Mooney wrote:
> I was trying out simple recursion as below, with the printout I
> expected, but it then also prints out "None" nine times. How does that
> work?
>
> Jim
>
> #Using Python 3.3.2 on Win 7 - standard project, standard test file
>
> def addone(num):
>      if num > 10:
>          return num
>      num = addone(num + 1)
        return None #Python puts the default return here as you don't 
specify it.
>
> print(addone(1))
>
> Result:
> 11
> None
> None
> None
> None
> None
> None
> None
> None
> None
>
>


-- 
"Steve is going for the pink ball - and for those of you who are 
watching in black and white, the pink is next to the green." Snooker 
commentator 'Whispering' Ted Lowe.

Mark Lawrence



More information about the Tutor mailing list