diff --git a/Armstrong_number b/ARMSTRONG_NUMBER.py similarity index 95% rename from Armstrong_number rename to ARMSTRONG_NUMBER.py index 7dd1b267ea0..34e8ee3d4db 100644 --- a/Armstrong_number +++ b/ARMSTRONG_NUMBER.py @@ -2,7 +2,6 @@ def is_armstrong_number(number): numberstr = str(number) length = len(numberstr) num = number - rev = 0 temp = 0 while num != 0: rem = num % 10 diff --git a/NUMBER_GUESSING_GAME.py b/NUMBER_GUESSING_GAME.py new file mode 100644 index 00000000000..8a2cf518bd7 --- /dev/null +++ b/NUMBER_GUESSING_GAME.py @@ -0,0 +1,70 @@ +""" +Module Name: Number Guessing Game +Description: A simple interactive game where the user sets an upper bound, + the program picks a random secret number within that range, + and the user guesses repeatedly until correct. Reports total + guesses taken at the end. +Author: Gaurav Sharma +Date: 2026-08-18 +""" + +import random + + +def get_upper_bound() -> int: + """Prompt the user for a valid upper bound. + + Returns: + An integer upper bound of 2 or greater. + """ + while True: + raw = input("Enter the upper bound for the range (e.g. 100): ") + if raw.isdigit() and int(raw) >= 2: + return int(raw) + print("Please enter an integer of 2 or greater.") + + +def get_guess(upper_bound: int) -> int: + """Prompt the user for a valid guess within the given range. + + Args: + upper_bound: The maximum valid value for a guess. + + Returns: + An integer guess between 1 and upper_bound, inclusive. + """ + while True: + raw = input(f"Your guess (1-{upper_bound}): ") + if raw.isdigit() and 1 <= int(raw) <= upper_bound: + return int(raw) + print(f"Enter a whole number between 1 and {upper_bound}.") + + +def play_game() -> None: + """Run a single round of the number guessing game. + + Selects a secret number, collects guesses until the correct value + is entered, and reports the total number of guesses taken. + """ + upper_bound = get_upper_bound() + secret_number = random.randint(1, upper_bound) + guesses = 0 + + print(f"\nI'm thinking of a number between 1 and {upper_bound}.") + + while True: + guess = get_guess(upper_bound) + guesses += 1 + + if guess < secret_number: + print("Too Low") + elif guess > secret_number: + print("Too High") + else: + print(f"\nCorrect! The number was {secret_number}.") + print(f"Total guesses: {guesses}") + return + + +if __name__ == "__main__": + play_game() \ No newline at end of file diff --git a/README.md b/README.md index 78bf507a22f..1c8406420ac 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Feel free to explore the scripts and use them for your learning and automation n 41. [Extract Thumbnail From Video](https://github.com/geekcomputers/Python/tree/ExtractThumbnailFromVideo) - Extract Thumbnail from video files 42. [How to begin the journey of open source (first contribution)](https://www.youtube.com/watch?v=v2X51AVgl3o) - First Contribution of open source 43. [smart_file_organizer.py](https://github.com/sangampaudel530/Python2.0/blob/main/smart_file_organizer.py) - Organizes files in a directory into categorized subfolders based on file type (Images, Documents, Videos, Audios, Archives, Scripts, Others). You can run it once or automatically at set intervals using the `--path` and `--interval` options. +44. [number_guessing.py](https://github.com/geekcomputers/Python/blob/master/number_guessing.py) - Interactive number guessing game where the user sets an upper bound and guesses a randomly generated number until correct.