0

I wrote this code in Python3 which I thought would return a random seven letter word (for a word guessing game) from a text file containing a long list of words. It does find such a word, but returns None. Can anyone explain why ?

from random import randrange
import os

def find_word():
        f = open('dictionary.txt')
        lines = f.readlines()
        x = randrange(1000)
        word = lines[x]
        word = word.rstrip(os.linesep)
        # removes the new line part of text, but the problem is the same with 
        # or without this line
        if len(word) == 7:
            print(word, type(word))
            return word
        else:
            find_word()
    
random_seven_letter_word = find_word()
print(random_seven_letter_word)
  • 1
    Close to a typo: you forgot the `return` when recursing. It should be: `if len(word) == 7: ... return word else: return find_word()` – Serge Ballesta Feb 16 '21 at 16:32
  • Does this answer your question? [Recursive function returning none in Python](https://stackoverflow.com/questions/19215141/recursive-function-returning-none-in-python) – Tomerikoo Feb 16 '21 at 16:55

2 Answers2

1

You need to return after the recursive call like this,

def find_word():
        f = open('dictionary.txt')
        lines = f.readlines()
        x = randrange(1000)
        word = lines[x]
        word = word.rstrip(os.linesep)
        # removes the new line part of text, but the problem is the same with 
        # or without this line
        if len(word) == 7:
            print(word, type(word))
            return word
        else:
            return find_word()
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
0

You are getting 'None', because you have used the print statement in the ending. You should just type the variable name, which is random_seven_letter_word(). You should even put the 2 brackets. So the final code will be -

def find_word():
        f = open('dictionary.txt')
        lines = f.readlines()
        x = randrange(1000)
        word = lines[x]
        word = word.rstrip(os.linesep)
        # removes the new line part of text, but the problem is the same with 
        # or without this line
        if len(word) == 7:
            print(word, type(word))
            return word
        else:
            return find_word()

random_seven_letter_word = find_word()
random_seven_letter_word()

Dharman
  • 30,962
  • 25
  • 85
  • 135
Boolean
  • 25
  • 7
  • For easily reproducible problems like this one, it would be best to test out your code before submitting it. Your function definition correctly fixes the the issue, but you made no mention of it in your description. Then your description proposes incorrect code and incorrectly diagnoses the problem. `find_word` returns a string, not a function, so this will result in an error when you try to call it as a function in your last line. The function returns `None` because there is no return statement, not because of the `print`. – General Grievance Feb 17 '21 at 18:58