Recursive function always returns None

Patrick LeGresley plegresl at stanford.edu
Mon Mar 8 20:16:41 EST 2004


Hi,

I have a sample piece of code for which I can't understand why it
returns the output it does.  I am running Python 2.3.2 on Redhat
Linux 8.0 (x86 hardware).  Here is the code and output:

granada 6% cat tmp.py
def test(astring):

        if (len(astring) >= 30):
                print 'astring =',astring
                return astring
        else:
                astring = astring + astring
                test(astring)

a = 'hello '
b = test(a)
print 'b =',b
granada 7%
granada 7%
granada 7% python tmp.py
astring = hello hello hello hello hello hello hello hello
b = None
granada 8%


So it seems to me that the value of b is wrong.  Could someone
explain if it is correct, why ?


Thanks,

Patrick



More information about the Python-list mailing list