Adding through recursion

Fredrik Lundh fredrik at
Fri Nov 18 16:02:44 CET 2005

martin.clausen at wrote:

> I still don't get it. I tried to test with x = 0 and found that to
> work. How come since the value of y is right and it is printed right it
> "turns into" None when returned by the return statement ?

because you're not returning the value to the print statement;
you're returning it to your own function, which throws it away.

if you add "raise Exception" to the "x == 0" path, you get this

$ python
Traceback (most recent call last):
  File "", line 10, in ?
    print add(2, 4)
  File "", line 8, in add
    add(x, y)
  File "", line 8, in add
    add(x, y)
  File "", line 4, in add
    raise Exception

which shows that you're a couple of levels down when you find
the right value.  and since the "add(x, y)" call at line 8 throws
away the result, the "print" statement at line 10 will never see


More information about the Python-list mailing list