diff --git a/Number_Guesser.py b/Number_Guesser.py new file mode 100644 index 00000000000..f6e7f9da2c4 --- /dev/null +++ b/Number_Guesser.py @@ -0,0 +1,31 @@ +import random +print(""" +=================================================================== + WELCOME TO THE NUMBER GUESSER GAME! +===================================================================""") +max_range = int(input("Enter the maximum range for the number guessing game: ")) +secret_number = random.randint(1, max_range) +# Flag variable to track if the number has been guessed or not +number_guessed = False +attempts = 0 +while not number_guessed: + try: + # To make sure the given input is an integer + guess = int(input(f"Guess a number between 1 and {max_range}: ")) + if guess < secret_number: + print("Too low! Try again.") + attempts += 1 # counts the number of incorrect attempts + elif guess > secret_number: + print("Too high! Try again.") + attempts += 1 # counts the number of incorrect attempts + else: + print(f"Congratulations! You've guessed the correct number! It was {secret_number}.\n") + print(f"Number of attempts: {attempts}") + number_guessed = True #Breaks the loop if the number is guessed correctly + + except ValueError: # prevents humanoid error if the user inputs a non-integer value + print("Invalid input! Please enter a valid integer.") +print(""" +=================================================================== + THANK YOU FOR PLAYING! +===================================================================""") \ No newline at end of file diff --git a/README.md b/README.md index 78bf507a22f..ec7b3886bfe 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_Guesser.py](https://github.com/1SKC1/Python.git) - A basic beginner friendly number guesser game with the user-specified range