Ad
Fundamentals
Strings

Simplified the function, but there's still room for improvement (for example, make it accept multiple types, like it is suggested by the test cases)

Code
Diff
  • std::string digest(const std::string param) {
        std::string result;
        for (char letter: param) {
          result += std::basic_string(1, letter) + " ";
        }
        result.pop_back();
        return result;
    }
    • using namespace std;
    • string digest(string param) {
    • string result;
    • for (int i = 0; i < param.size() - 1; i++) {
    • result += param[i];
    • result += ' ';
    • std::string digest(const std::string param) {
    • std::string result;
    • for (char letter: param) {
    • result += std::basic_string(1, letter) + " ";
    • }
    • result += param[param.size() - 1];
    • result.pop_back();
    • return result;
    • }

Replaced the pronoun 'he' for 'they'

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):
                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!')
    
                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 play_again():
        # Ask the player if they want 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!')
    • 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 play_again():
    • # Ask the player if he wants to play again
    • # Ask the player if they want 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()

Made some simple refactoring, like only importing randint from random instead of the whole library, not using sys.exit at all, made the play_again function only return True or False, instead of handling the the script exit, and fixing a game-breaking bug that the secret_number was a constant, instead of being generated in every game.

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):
                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!')
    
                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 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()
    
    • import random
    • import sys
    • from random import randint
    • # Set up constants
    • SECRET_NUMBER = random.randint(1, 100)
    • GUESS_LIMIT = 10
    • 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!')
    • 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
    • if int(guess) == secret_number:
    • print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
    • 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')
    • break
    • elif int(guess) > secret_number:
    • print(int(guess), secret_number)
    • print(f"Too low!")
    • if int(guess) > SECRET_NUMBER:
    • print('Too high!')
    • else:
    • print("Too high!")
    • else:
    • print('Too low!')
    • play_again()
    • print(f'Game over! The secret number was {secret_number}.')
    • if play_again() is False:
    • break
    • def play_again():
    • """Play again options"""
    • # Ask the player if he wants to play again
    • while True:
    • user_input = input('Do you want to play again (yes or no)?
    • > ').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()
    • user_input = input('Do you want to play again (yes or no)?
    • > ')
    • 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()