Ad

While using function maybe enough to make this game. I actually made a class for the game to organize it further. You can easily change the limit and range of the game while creating the object of the class.
(Execute the code on your local machine, I don't think codewars console can take input from user.)

Code
Diff
  • import random
    
    class GuessGame():
        def __init__(self, limit, mn, mx):
            self.guess_limit = limit
            self.guess_interval = (mn, mx)
    
        def read_guess(self):
            while True:
                try:
                    value = int(input("\nGuess: "))
                    break
                except ValueError:
                    print("\nInvalid Input")
            return value
    
        def start_match(self):
            print(f'Guess a number between {self.guess_interval[0]} and {self.guess_interval[1]}')
    
            secret_number = random.randint(*self.guess_interval)
            guess_count = 0
    
            while guess_count < self.guess_limit:
                guess = self.read_guess()
                guess_count += 1
    
                if guess > secret_number:
                    print('\nNo, too big.')
                elif guess < secret_number:
                    print('\nNo, too small.')
                else:
                    print('\nWow, you are a actully are a true mind reader!')
                    print(f"The number was {secret_number} indeed! ")
                    return
    
            print("Sorry, you're out of guesses!")
    
    
    def game_loop(GameObj):
        replay = "yes"
        while replay != "no":
            replay = input('\nDo you want to play Guess game again? (yes/no) - ').lower()
            if replay == 'yes':
                GameObj.start_match()
            elif replay != 'no':
                print("I don't understand")
    
            print('\nBye! See you soon!')
    
    
    Game = GuessGame(5, 1, 25)      # Change limit and interval size here!
    game_loop(Game)
    • import random
    • def read_guess():
    • value = None
    • while value is None:
    • try:
    • value = int(input('
    • Guess: '))
    • except ValueError:
    • print("
    • Invalid Input!")
    • return value
    • def start_match(guess_limit, guess_interval):
    • print(f'Guess a number between {guess_interval[0]} and {guess_interval[1]}')
    • secret_number = random.randint(*guess_interval)
    • guess_count = 0
    • while guess_count < guess_limit:
    • guess = read_guess()
    • guess_count += 1
    • if guess > secret_number:
    • print('
    • No, too big.')
    • elif guess < secret_number:
    • print('
    • No, too small.')
    • else:
    • print('
    • Wow, you are a actully are a true mind reader!')
    • print(f"The number was {secret_number} indeed! ")
    • return
    • print("Sorry, you're out of guesses!")
    • def game_loop():
    • guess_limit = 5
    • guess_interval = (1, 25)
    • start_match(guess_limit, guess_interval)
    • replay = 'yes'
    • while replay != 'no':
    • replay = input('
    • Do you want to play Guess game again? (yes/no) - ')
    • class GuessGame():
    • def __init__(self, limit, mn, mx):
    • self.guess_limit = limit
    • self.guess_interval = (mn, mx)
    • def read_guess(self):
    • while True:
    • try:
    • value = int(input("
    • Guess: "))
    • break
    • except ValueError:
    • print("
    • Invalid Input")
    • return value
    • def start_match(self):
    • print(f'Guess a number between {self.guess_interval[0]} and {self.guess_interval[1]}')
    • secret_number = random.randint(*self.guess_interval)
    • guess_count = 0
    • while guess_count < self.guess_limit:
    • guess = self.read_guess()
    • guess_count += 1
    • if guess > secret_number:
    • print('
    • No, too big.')
    • elif guess < secret_number:
    • print('
    • No, too small.')
    • else:
    • print('
    • Wow, you are a actully are a true mind reader!')
    • print(f"The number was {secret_number} indeed! ")
    • return
    • print("Sorry, you're out of guesses!")
    • def game_loop(GameObj):
    • replay = "yes"
    • while replay != "no":
    • replay = input('
    • Do you want to play Guess game again? (yes/no) - ').lower()
    • if replay == 'yes':
    • start_match(guess_limit, guess_interval)
    • GameObj.start_match()
    • elif replay != 'no':
    • print("I don't understand")
    • print('\nBye! See you soon!')
    • game_loop()
    • print('\nBye! See you soon!')
    • Game = GuessGame(5, 1, 25) # Change limit and interval size here!
    • game_loop(Game)