Ad
Code
Diff
  • from random import randint
    
    def main():
        GUESS_LIMIT = 10
        while True:
            secret_number = randint(1, 100)
    
            for guess_count in range(GUESS_LIMIT):
                
                validating_userinput()
    
                if int(guess) == secret_number:
                    print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
                    break
    
                elif int(guess) > secret_number:
                    print(int(guess), secret_number)
                    print(f"Too low!")
    
                else:
                    print("Too high!")
    
            else:
                print(f'Game over! The secret number was {secret_number}.')
        
            if play_again() is False:
                break
                
    def validating_userinput():
        """Verifying users input"""
        guess = 0
        while True:
            guess += input('Guess a number between 1 and 100 \n> ')
            if guess.isdigit() and 1 <= int(guess) <= 100:
                break
            else:
                print('Invalid input!')
        return guess
            
        
            
    def play_again():
        # Ask the player if he wants to play again
        while True:
            user_input = input('Do you want to play again (yes or no)?\n> ')
            if user_input.isalpha():
                if user_input.lower() == 'yes':
                    return True
                elif user_input.lower() == "no":
                    return False
                
            print('Invalid input!')
    
    
    if __name__ == '__main__':
        main()
    
    • from random import randint
    • def main():
    • GUESS_LIMIT = 10
    • while True:
    • secret_number = randint(1, 100)
    • for guess_count in range(GUESS_LIMIT):
    • validating_userinput()
    • if int(guess) == secret_number:
    • print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
    • break
    • elif int(guess) > secret_number:
    • print(int(guess), secret_number)
    • print(f"Too low!")
    • else:
    • print("Too high!")
    • else:
    • print(f'Game over! The secret number was {secret_number}.')
    • if play_again() is False:
    • break
    • def validating_userinput():
    • """Verifying users input"""
    • guess = 0
    • while True:
    • guess += input('Guess a number between 1 and 100 \n> ')
    • if guess.isdigit() and 1 <= int(user_num) <= 100:
    • if guess.isdigit() and 1 <= int(guess) <= 100:
    • break
    • else:
    • print('Invalid input!')
    • return guess
    • def play_again():
    • # Ask the player if he wants to play again
    • while True:
    • user_input = input('Do you want to play again (yes or no)?\n> ')
    • if user_input.isalpha():
    • if user_input.lower() == 'yes':
    • return True
    • elif user_input.lower() == "no":
    • return False
    • print('Invalid input!')
    • if __name__ == '__main__':
    • main()

I created a simple function that handles the validation of the user input.

Code
Diff
  • from random import randint
    
    def main():
        GUESS_LIMIT = 10
        while True:
            secret_number = randint(1, 100)
    
            for guess_count in range(GUESS_LIMIT):
                
                validating_userinput()
    
                if int(guess) == secret_number:
                    print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
                    break
    
                elif int(guess) > secret_number:
                    print(int(guess), secret_number)
                    print(f"Too low!")
    
                else:
                    print("Too high!")
    
            else:
                print(f'Game over! The secret number was {secret_number}.')
        
            if play_again() is False:
                break
    
    def validating_userinput():
        guess = 0
        while True:
            guess += input('Guess a number between 1 and 100 \n> ')
            if guess.isdigit() and 1 <= int(user_num) <= 100:
                break
            else:
                print('Invalid input!')
                
        return guess
            
        
            
    def play_again():
        # Ask the player if he wants to play again
        while True:
            user_input = input('Do you want to play again (yes or no)?\n> ')
            if user_input.isalpha():
                if user_input.lower() == 'yes':
                    return True
                elif user_input.lower() == "no":
                    return False
                
            print('Invalid input!')
    
    
    if __name__ == '__main__':
        main()
    
    • from random import randint
    • def main():
    • GUESS_LIMIT = 10
    • while True:
    • secret_number = randint(1, 100)
    • for guess_count in range(GUESS_LIMIT):
    • while True:
    • # Validating user input
    • guess = input('Guess a number between 1 and 100 \n> ')
    • if guess.isdigit() and 1 <= int(guess) <= 100:
    • break
    • else:
    • print('Invalid input!')
    • validating_userinput()
    • if int(guess) == secret_number:
    • print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
    • break
    • elif int(guess) > secret_number:
    • print(int(guess), secret_number)
    • print(f"Too low!")
    • else:
    • print("Too high!")
    • else:
    • print(f'Game over! The secret number was {secret_number}.')
    • if play_again() is False:
    • break
    • def validating_userinput():
    • guess = 0
    • while True:
    • guess += input('Guess a number between 1 and 100 \n> ')
    • if guess.isdigit() and 1 <= int(user_num) <= 100:
    • break
    • else:
    • print('Invalid input!')
    • return guess
    • def play_again():
    • # Ask the player if he wants to play again
    • while True:
    • user_input = input('Do you want to play again (yes or no)?\n> ')
    • if user_input.isalpha():
    • if user_input.lower() == 'yes':
    • return True
    • elif user_input.lower() == "no":
    • return False
    • print('Invalid input!')
    • if __name__ == '__main__':
    • main()