-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2025 from IndraniSom/Indrani
added a number guessing game
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import random | ||
|
||
def number_guessing_game(): | ||
|
||
secret_number = random.randint(1, 100) | ||
attempts = 0 | ||
|
||
print("Welcome to the Number Guessing Game!") | ||
print("I'm thinking of a number between 1 and 100.") | ||
|
||
while True: | ||
try: | ||
guess = int(input("Your guess: ")) | ||
attempts += 1 | ||
|
||
if guess < secret_number: | ||
print("Too low! Try again.") | ||
elif guess > secret_number: | ||
print("Too high! Try again.") | ||
else: | ||
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts!") | ||
break | ||
|
||
except ValueError: | ||
print("Please enter a valid number.") | ||
|
||
if __name__ == "__main__": | ||
number_guessing_game() |