[Tutor] Recursion always returns None

Dharmit Shah shahdharmit at gmail.com
Tue Aug 28 17:51:55 CEST 2012


Hello,

@Hugo Arts : Thank you! That was awesome to read. Thanks for the len()
suggestion.

@ Steve : Thank you. As suggested by Dave Angel, I am going to try the
loop. And even before implementing it, I can feel that it's going to
be more efficient than recursion.

@Dave Angel : Thank you for the loop idea. It didn't strike me at all.

@All : Thanks a bunch for helping me out.

:)

Cheers,
Dharmit


On Tue, Aug 28, 2012 at 7:18 PM, Dave Angel <d at davea.name> wrote:
> On 08/28/2012 07:23 AM, Dharmit Shah wrote:
>> Hello,
>>
>> I am trying to do the following :
>>
>> 1) Ask user for the length of the word that he'd like to guess (for
>> hangman game).
>> 2) Pick a random word from /usr/share/dict/words (which I understand
>> is not the best choice for hangman).
>> 3) Call a function that would pick a random word to proceed further.
>>
>> Below is the code for the part I described above :
>>
>> [code]
>>
>> #!/bin/env python
>> import random
>>
>> def pick_random(l, ln):           # picks a random word from the list
>> l of length ln
>>     global mystery
>>     word = random.choice(l)
>>     if word.__len__() != ln:
>>         pick_random(l, ln)        # recursion
>>     else:
>>         print "Should return %s" % word     # prints the chosen random
>> word correctly
>>         return word                                      # always
>> return None, why? :(
>
> There's no return statement here, to cover the case where the if-clause
> succeeded.
>
>> if __name__ == "__main__":
>>     ln = raw_input("How long word can you guess (number of alphabets) : ")
>>     ln = int(ln)
>>     l = []
>>     with open("/usr/share/dict/words", "r") as f:
>>         for i in f.readlines():
>>             i = i.split("\n")[0]
>>             if i.isalpha():
>>                 l.append(i)
>>
>>     word = pick_random(l, ln)
>>     print word
>>
>> [/code]
>>
>> Sample output :
>>
>> $ python hangman.py
>> How long word can you guess (number of alphabets) : 6
>> Should return inarch
>> None
>> $
>>
>> The problem is that the last line "print word" always prints None. I
>> know I am doing something wrong in the recursion part of the function
>> "pick_random". Can someone please point what I am missing. Thank you!
>>
>> Cheers,
>> Dharmit
>>
>
> There are two things wrong, one of which has already been pointed out.
> But the most fundamental thing that's wrong is that once you have called
> the recursion, you don't return a value at all, simply falling off the
> end of the function.  Python returns a value of None when you omit the
> return statement.
>
> So you should add a statement 'return word', which will eliminate the
> None.  But of course it'll be the wrong word.  To fix that, you need to
> assign the results of the call to pick_random() to the same local
> variable, word.
>
> As others have pointed out, this is a poor choice for recursion.
> Recursion can be more readable for some problems, when the problem
> statement is naturally recursive.  But even then, it can frequently lead
> to stack overruns, and performance problems.  But in this case a simple
> loop would make much more sense.  So unless the instructor is requiring
> you to use recursion, please redo it as a loop.
>
> While we're at it, please use the len() function, rather than __len__()
> method.  And instead doing a split() method for eliminating the
> linefeeds, what you really want to do is rstrip().
>
>
>
> --
>
> DaveA
>



-- 
Dharmit Shah
www.about.me/dharmit


More information about the Tutor mailing list