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?

Pablo
  • 465
  • 1
  • 4
  • 14
  • Does this answer your question? [Recursion in Python? RuntimeError: maximum recursion depth exceeded while calling a Python object](https://stackoverflow.com/questions/14222416/recursion-in-python-runtimeerror-maximum-recursion-depth-exceeded-while-callin) – sushanth May 17 '20 at 07: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. – aks May 17 '20 at 07: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 – David Buck May 17 '20 at 07:18
  • 2
    Don't do recursion, use a while loop – azro May 17 '20 at 07:18
  • Maybe it is a recursion class, and it is a non-functional requirement to solve the question using recursion. – Danizavtz May 17 '20 at 07:27

3 Answers3

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

QuantAC
  • 31
  • 1
  • 9
  • 1
    This won’t help in general. – quamrana May 17 '20 at 07:22
  • 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 – QuantAC May 17 '20 at 07:30
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

Aditya Kurkure
  • 422
  • 5
  • 19
-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)
azro
  • 53,056
  • 7
  • 34
  • 70