0

So I created a simple Number guessing game to get back into the python Syntax. While creating it, I ran into the problem that I had to return self.get_guess() instead of just calling it from the method itself recursively.

So my question is, why I need to return the function.

The method I am referring too:

def get_guess(self):
        str_guess = input(
            f'Please enter a number between {self.minimum} and {self.maximum}: ')
        if self.validate_guess(str_guess):
            return int(str_guess)
        else:
            print(
                'Invalid guess, please enter an integer and a number in the given range')
            return self.get_guess()

How I call it:

def play(self):
        while True:
            self.number_of_guesses += 1

            guess = self.get_guess()

The complete Code for better understanding

class GuessNumber():
    def __init__(self, correct_number, minimum=0, maximum=100):
        self.number_of_guesses = 0
        self.correct_number = correct_number
        self.minimum = minimum
        self.maximum = maximum

    def get_guess(self):
        str_guess = input(
            f'Please enter a number between {self.minimum} and {self.maximum}: ')
        if self.validate_guess(str_guess):
            return int(str_guess)
        else:
            print(
                'Invalid guess, please enter an integer and a number in the given range')
            return self.get_guess()

    def validate_guess(self, str_number):
        try:
            guess = int(str_number)
        except:
            return False

        return self.minimum <= guess <= self.maximum

    def play(self):
        while True:
            self.number_of_guesses += 1

            guess = self.get_guess()

            if guess < self.correct_number:
                print('Your guess was under.')
            elif guess > self.correct_number:
                print('Your guess was over.')
            else:
                break

        print(
            f'You guessed the correct number in {self.number_of_guesses} guesses')


guess_game = GuessNumber(correct_number=33, minimum=0, maximum=100)
guess_game.play()

The method get_guess gets called in play()

It also works without returning the function call as long as I don't hit an Exception by f.e. typing in a letter as a string.

As soon as I type in a String and I don't return the function I get the following error: TypeError: '<' not supported between instances of 'NoneType' and 'int'. So it seems like the new input doesn't get stored somehow

Thank you for your help!

David Haase
  • 179
  • 1
  • 8
  • 1
    Because a function doesn't return anything unless it has an explicit `return` statement. Doesn't matter whether it's recursive or not. – deceze Apr 20 '21 at 14:27
  • I get that, but what I thought what would happen is that I just call the `get_guess` function until the conversion from string to int is valid and I then return the guess as an int. – David Haase Apr 20 '21 at 14:32
  • 2
    It doesn’t make sense to use recursion for this. Instead, I would just use a loop. Unfortunately the question is closed as a duplicate (I don’t think it is really a duplicate of the linked question, though), so I can’t post code. – inof Apr 20 '21 at 14:32
  • A recursive function isn't in any way special. `def foo(): bar()` — Would you expect this function to return anything? No. — `def foo(): foo()` Recursion doesn't change that, this function still only calls itself without returning anything. You're building up a call chain; `foo` calls `bar`, `bar` returns to `foo`. Recursion doesn't change that: `foo` calls `foo`, and that latter returns only to the former. – deceze Apr 20 '21 at 15:04
  • You can't simply return from any depth of a recursive function. The return value has to pop up through the call stack in the form of return values. Each time you call from a function to the next level deep, you have to deal with the function return from that call, and probably return it up to your own caller. – RufusVS Apr 20 '21 at 19:00

0 Answers0