From 70ae268355f93f86f24d6aabe67b37b0235adfa5 Mon Sep 17 00:00:00 2001 From: tech-z <152055911+Achi-Vyshnavi@users.noreply.github.com> Date: Fri, 26 Dec 2025 20:31:04 +0530 Subject: [PATCH] Enhance Number-Guess game: add levels, score system, attempts limit, and replay This update significantly improves the Number-Guess game by introducing multiple new features: 1. **Levels with increasing difficulty:** Each new level increases the range of numbers to guess, making the game progressively harder. 2. **Score system:** Players earn points based on remaining attempts per level, rewarding faster correct guesses. 3. **Attempts limit:** Each level allows a maximum of 5 attempts to guess the number, adding challenge and excitement. 4. **Replay / Continue option:** After completing a level, the player can choose to continue to the next level or quit the game. 5. **Improved input validation:** Ensures only positive integers are accepted, making the game more robust and user-friendly. 6. **Cleaner and modular code:** Functions are modular, easy to read, and maintain. These enhancements make the game more engaging, interactive, and recruiter-friendly, demonstrating the ability to improve existing code and add meaningful features. --- Number-Guess/number-guess.py | 84 ++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/Number-Guess/number-guess.py b/Number-Guess/number-guess.py index 6922dd9..e5365cf 100644 --- a/Number-Guess/number-guess.py +++ b/Number-Guess/number-guess.py @@ -1,47 +1,57 @@ from random import randrange + def main(): - randomized_no = randomize_no() + print("Welcome to Number Guessing Game!") + level = 1 + total_score = 0 + while True: - user_guessed_input = guess() - if int(user_guessed_input) > randomized_no: - print("Too large!") - continue - elif int(user_guessed_input) < randomized_no: - print("Too small!") - continue + print(f"\n--- Level {level} ---") + max_number = get_level_max(level) + randomized_no = randrange(1, max_number + 1) + attempts_left = 5 # max attempts per level + + while attempts_left > 0: + user_guess = get_guess(level) + if user_guess > randomized_no: + print("Too high! Try again.") + elif user_guess < randomized_no: + print("Too low! Try again.") + else: + print(f"Correct! You've cleared Level {level}.") + total_score += attempts_left * 10 # more points for fewer attempts + break + attempts_left -= 1 + print(f"Attempts left: {attempts_left}") else: - print("Just right!") + print(f"Game Over! The number was {randomized_no}.") break + + level += 1 + print(f"Total Score: {total_score}") + cont = input("Do you want to continue to the next level? (y/n): ").lower() + if cont != 'y': + print(f"Thanks for playing! Final Score: {total_score}") + break + def is_positive_integer(n): - """This function takes an input and check if the user input is an integer""" + try: + num = int(n) + return num > 0 + except ValueError: + return False + +def get_guess(level): while True: - try: - num = int(n) - if int(num) < 1: - return False - except ValueError: - return False + guess_input = input(f"Level {level} Guess: ") + if is_positive_integer(guess_input): + return int(guess_input) else: - return True -def get_user_input(): - """Prompt the user for an input and check if it's a positive integer""" - while True: - user_input = input("Level 1: ") - if (is_positive_integer(user_input)): - return int(user_input) -def guess(): - "Prompt the user for an input guess, and check if it's an integer" - while True: - user_guess = input("Guess: ") - if is_positive_integer(user_guess): - return user_guess -def randomize_no(): - """Randomize number""" - user_inputted_number = get_user_input() - if int(user_inputted_number) > 1: - random_number = randrange(1, user_inputted_number) - return random_number - else: - return int(user_inputted_number) + print("Enter a valid positive integer!") + +def get_level_max(level): + """Increase range as levels go up""" + return 10 + (level - 1) * 5 + if __name__ == "__main__": main()