-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_guessing_game.py
29 lines (23 loc) · 950 Bytes
/
number_guessing_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import random
def number_guessing_game():
# Generate a random number between 1 and 100
target_number = random.randint(1, 100)
attempts = 0
guessed_number = None
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
while guessed_number != target_number:
try:
guessed_number = int(input("Take a guess: "))
attempts += 1
if guessed_number < target_number:
print("Too low! Try again.")
elif guessed_number > target_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
print(f"The number was {target_number}.")
except ValueError:
print("Please enter a valid number.")
if __name__ == "__main__":
number_guessing_game()