Adding through recursion

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


martin.clausen at gmail.com 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
traceback:

$ python add.py
6
Traceback (most recent call last):
  File "add.py", line 10, in ?
    print add(2, 4)
  File "add.py", line 8, in add
    add(x, y)
  File "add.py", line 8, in add
    add(x, y)
  File "add.py", line 4, in add
    raise Exception
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
it.

</F>






More information about the Python-list mailing list