[Tutor] recursion surprise

Thomas Murphy thomasmurphymusic at gmail.com
Sun Jun 9 00:05:12 CEST 2013


Hi Jim,

First off, I'm a beginner, so I offer this with humility, but are you
sure you wanted a > then sign? It looks to me like you want to loop
through and print each number while num is less than 10 right?

I went with this, using while loop instead:


def addOne(num):
    while num < 10:
        print num
        num = num + 1

addOne(1)

On Sat, Jun 8, 2013 at 5:46 PM, Jim Mooney <cybervigilante at gmail.com> 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)
>
> print(addone(1))
>
> Result:
> 11
> None
> None
> None
> None
> None
> None
> None
> None
> None
>
>
> --
> Jim
> "Would you use Internet Explorer if someone put a gun to your head?"
> "How big is the gun?"
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
Sincerely,
Thomas Murphy
Code Ninja
646.957.6115


More information about the Tutor mailing list