0

I have a function which is supposed to get a word and choose one of its letters by random. I also have a list of already chosen letters. If the chosen letter already exists in this list, I use recursion to choose another letter:

def choose(word, chosen_list):
    chosen_letter = random.choice(word)
    if chosen_letter not in chosen_list:
        print("Found")
    else:
        choose(word, chosen_list)

The problem is that when choose function is called multiple times, I encounter an error:

chosen_letter = random.choice(word)
  File "...\random.py", line 259, in choice
    i = self._randbelow(len(seq))
  File "...\random.py", line 232, in _randbelow
    if type(random) is BuiltinMethod or type(getrandbits) is Method:
RecursionError: maximum recursion depth exceeded while calling a Python object

Why this is happening?

5
  • Does this answer your question? Recursion in Python? RuntimeError: maximum recursion depth exceeded while calling a Python object Commented May 17, 2020 at 7:16
  • I think it's the infinite(too deep) recursion in choose(word, chosen_list) - try printing the chosen_letter every time on console and you should be able to debug and see how many times it's getting called. sidenote: there should be a more deterministic way to solve this. Commented May 17, 2020 at 7:17
  • 1
    Why use recursion for this? If you pick a word where all of the letters are in the list, it has no means to escape Commented May 17, 2020 at 7:18
  • 2
    Don't do recursion, use a while loop Commented May 17, 2020 at 7:18
  • Maybe it is a recursion class, and it is a non-functional requirement to solve the question using recursion. Commented May 17, 2020 at 7:27

3 Answers 3

0

In python recursion is limited (to around 1000 calls I think). This is done to avoid infinite calls. You can overcome this by increasing the recursion limit using the sys module. Try:

import sys

sys.setrecursionlimit(some_numerical_limit)

For more information, you can visit this geeksforgeeks article

Sign up to request clarification or add additional context in comments.

2 Comments

This won’t help in general.
Or else, you can remove the letter from the word before calling it again. This way it won't pick the letter that's already been ruled out. You can write it in else section else: word= word.replace(chosen_letter,'') this way, you won't run into depth exceeded error
0

Consider the case where all the letters are in the chosen list.Also if the selected letter is already in the chosen list you should remove it. Here is an example

    import random
    def choose(word, chosen_list):
        if len(word)>0:
          chosen_letter = random.choice(word)
          if chosen_letter not in chosen_list:
             print("Found")
          else:
             choose(word.replace(chosen_letter,""), chosen_list)
        else:
          print("end")

Here we are removing the letter that is already in the list. If no such letter exists the program will print end

Comments

-1

Don't use recursion, this is not appropriate here.


Use a while loop, to pick a letter until it's not in the given list (give it a clearer name)

def choose(word, exclude_list):
    chosen_letter = random.choice(word)
    while chosen_letter in exclude_list:
        chosen_letter  = random.choice(word)
    print("Found a letter that is not in list", chosen_letter)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.