Ad

Removed an extrenuous while loop from your code. If you want to remove the play again while loop, you should use a enum to keep track of the state.

Also there are no nested ifs here which is nice

Code
Diff
  • import random
    import sys
    
    # Set up constants
    SECRET_NUMBER = random.randint(1, 100)
    GUESS_LIMIT = 10
    
    
    def main():
        """Main game function."""
    
        guess_count = 0
        while True:  # main game loop
            guess = input('Guess a number between 1 and 100\n> ')
    
            if not guess.isdigit() or int(guess) not in range(1, 101):
                print('Invalid input!')
                continue
    
            guess_count += 1
            if guess_count == GUESS_LIMIT:
                print(f'Game over! The secret number was {SECRET_NUMBER}.')
            elif int(guess) == SECRET_NUMBER:
                print(f'Congratulations! You guessed the secret number in {guess_count} guesses')
            elif int(guess) > SECRET_NUMBER:
                print('Too high!')
                continue
            else: # int(guess) < SECRET_NUMBER:
                print('Too low!')
                continue
    
            if play_again():
                continue
            print('Thanks for playing, Goodbye!')
            sys.exit()
    
    
    def play_again():
        """Play again options"""
        while True:
            user_input = input('Do you want to play again (yes or no)?\n> ').lower()
            if user_input not in ['yes', 'no']:
                print('Invalid input!')
                continue
            return user_input == 'yes'
    
    
    if __name__ == '__main__':
        main()
    
    
    • import random
    • import sys
    • # Set up constants
    • SECRET_NUMBER = random.randint(1, 100)
    • GUESS_LIMIT = 10
    • def main():
    • """Main game function."""
    • guess_count = 0
    • while True: # main game loop
    • while True: # Validate user input
    • guess = input('Guess a number between 1 and 100
    • > ')
    • if not guess.isdigit() or int(guess) not in range(1, 101):
    • print('Invalid input!')
    • continue
    • else:
    • guess_count += 1
    • break
    • guess = input('Guess a number between 1 and 100
    • > ')
    • if not guess.isdigit() or int(guess) not in range(1, 101):
    • print('Invalid input!')
    • continue
    • guess_count += 1
    • if guess_count == GUESS_LIMIT:
    • print(f'Game over! The secret number was {SECRET_NUMBER}.')
    • break
    • if int(guess) == SECRET_NUMBER:
    • elif int(guess) == SECRET_NUMBER:
    • print(f'Congratulations! You guessed the secret number in {guess_count} guesses')
    • break
    • if int(guess) > SECRET_NUMBER:
    • elif int(guess) > SECRET_NUMBER:
    • print('Too high!')
    • else:
    • continue
    • else: # int(guess) < SECRET_NUMBER:
    • print('Too low!')
    • continue
    • play_again()
    • if play_again():
    • continue
    • print('Thanks for playing, Goodbye!')
    • sys.exit()
    • def play_again():
    • """Play again options"""
    • while True:
    • user_input = input('Do you want to play again (yes or no)?\n> ').lower()
    • if user_input not in ['yes', 'no']:
    • print('Invalid input!')
    • continue
    • else:
    • break
    • if user_input == 'yes':
    • return main()
    • else:
    • print('Thanks for playing, Goodbye!')
    • sys.exit()
    • return user_input == 'yes'
    • if __name__ == '__main__':
    • main()