Move History

Rooted by: Guess Game
Fork Selected
  • Code
    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\n> ')
                if not guess.isdigit() or int(guess) not in range(1, 101):
                    print('Invalid input!')
                    continue
                else:
                    guess_count += 1
                    break
    
            if guess_count == GUESS_LIMIT:
                print(f'Game over! The secret number was {SECRET_NUMBER}.')
                break
    
            if int(guess) == SECRET_NUMBER:
                print(f'Congratulations! You guessed the secret number in {guess_count}/{GUESS_LIMIT} guesses')
                break
            if int(guess) == SECRET_NUMBER and guess_count < 2:
                print(f'Flawless victory!')
    
            if int(guess) > SECRET_NUMBER:
                print('Too high!')
    
            else:
                print('Too low!')
    
        play_again()
    
    
    def play_again():
        """Play again options"""
        while True:
            if input('Do you want to play again (yes or no)?\n> ').lower() not in ['yes', 'no']:
                print('Invalid input!')
                continue
            else:
                break
        if user_input == 'yes':
            return main()
        else:
            print('Thanks for playing, Goodbye!')
            sys.exit()
    
    
    if __name__ == '__main__':
        main()
    
    Test Cases
    import codewars_test as test
    # TODO Write tests
    import solution # or from solution import example
    
    # test.assert_equals(actual, expected, [optional] message)
    @test.describe("Example")
    def test_group():
        @test.it("test case")
        def test_case():
            test.assert_equals(1 + 1, 2)
    
  • Code
    • 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\n> ')
    • if not guess.isdigit() or int(guess) not in range(1, 101):
    • print('Invalid input!')
    • continue
    • else:
    • guess_count += 1
    • break
    • if guess_count == GUESS_LIMIT:
    • print(f'Game over! The secret number was {SECRET_NUMBER}.')
    • break
    • if int(guess) == SECRET_NUMBER:
    • print(f'Congratulations! You guessed the secret number in {guess_count} guesses')
    • print(f'Congratulations! You guessed the secret number in {guess_count}/{GUESS_LIMIT} guesses')
    • break
    • if int(guess) == SECRET_NUMBER and guess_count < 2:
    • print(f'Flawless victory!')
    • if int(guess) > SECRET_NUMBER:
    • print('Too high!')
    • else:
    • print('Too low!')
    • play_again()
    • def play_again():
    • """Play again options"""
    • while True:
    • user_input = input('Do you want to play again (yes or no)?
    • > ').lower()
    • if user_input not in ['yes', 'no']:
    • if input('Do you want to play again (yes or no)?
    • > ').lower() not in ['yes', 'no']:
    • print('Invalid input!')
    • continue
    • else:
    • break
    • if user_input == 'yes':
    • return main()
    • else:
    • print('Thanks for playing, Goodbye!')
    • sys.exit()
    • if __name__ == '__main__':
    • main()