0

I'm writing code for a number guessing game and it has to be sold by recursion. But when I execute it I get this error: maximum recursion depth exceeded. Why is that?

This is my code:

import random
n = random.randrange(0,100)
guess = int(input("Introduce a number: "))
def game(guess):
    if guess == n:
        print("Your guess is correct.")
    elif guess > n:
        print("Your guess is too high")
        game(guess)
    elif guess < n:
        print("Your guess is too low")
        game(guess)
game(guess)
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Sebastian
  • 57
  • 1
  • 2
  • 8

4 Answers4

1

The reason is that, unless guess is equal to n the first time you call the function, you have an infinite recursion because you call game with the same value of guess. You don't provide any way to stop the recursion.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

Your game function don't need any parameter. You need to use else instead of last elif and guess = int(input("Introduce a number: ")) step should be in your game function (Tested):

import random
n = random.randrange(0,100)
def game():
    guess = int(input("Introduce a number: "))
    if guess == n:
        print("Your guess is correct.")
    elif guess > n:
        print("Your guess is too high")
        game()
    else:
        print("Your guess is too low")
        game()
game()
mertyildiran
  • 6,477
  • 5
  • 32
  • 55
  • 2
    That won't work well, if you're terrible at this game, you'll eventually blow the stack. – Jeff Mercado Oct 11 '15 at 23:34
  • @JeffMercado I'm sure Sebastian's teacher wants a recursive function for his assignment :) – mertyildiran Oct 11 '15 at 23:40
  • That would be a stretch. Recursion has no place here, I doubt any competent teacher would promote writing code in a way that is destined to fail. – Jeff Mercado Oct 11 '15 at 23:43
  • @JeffMercado This is the basic assignment for understanding how a recursive function works. I saw it many times in engineering departments :) – mertyildiran Oct 11 '15 at 23:47
  • 1
    Ah, I missed the part in the question that says recursion is required (sorta). Anyway, there's better ways to teach recursion, and this is not the use case for it. Oh well. – Jeff Mercado Oct 11 '15 at 23:50
  • I managed to fix this. I'm dumb and I didn't notice I wasn't asking for a new input so it kept looping with the first guess so it would obviously crash. But I've managed to fix it, I even managed to add a guess counter so it's all good. Thanks to everyone for the help. – Sebastian Oct 12 '15 at 03:41
0

maximum recursion depth exceeded occurs because of the infinite loops when guess > n , guess < n condition is meet. To know further refer to this question.

The below code should work as expected.

import random,sys
n = random.randrange(0,100)

def game(guess):
    if guess == n:
        print("Your guess is correct.")
        sys.exit()
    elif guess > n:
        print("Your guess is too high")
    elif guess < n:
        print("Your guess is too low")

while True:
    guess = int(input("Introduce a number: "))
    game(guess)
Community
  • 1
  • 1
  • 1
    I think Sebastian's teacher wants a recursive function for his assignment :) Then your answer is not good for him :D – mertyildiran Oct 11 '15 at 23:43
0
  1. You need use random.randint() function like this: n = random.randint(0, 100).
  2. Use a while loop is recommended.
  3. You didn't call guess = int(input("Introduce a number: ")) again.

import random
n = random.randint(0, 100)
guess = int(input("Introduce a number: "))

def game(guess):
    while guess != n:
        if guess > n:
            print("Your guess is too high")
        elif guess < n:
            print("Your guess is too low")
        guess = int(input("Introduce a number: "))

    else:
        print("Your guess is correct.")

game(guess)
Remi Guan
  • 21,506
  • 17
  • 64
  • 87