-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex-15.py
28 lines (23 loc) · 858 Bytes
/
ex-15.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
import random
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
lower_bound = 1
upper_bound = 100
number_to_guess = random.randint(lower_bound, upper_bound)
print(f"I'm thinking of a number between {lower_bound} and {upper_bound}.")
attempts = 0
while True:
try:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < number_to_guess:
print("Too low! Try again.")
elif guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
except ValueError:
print("Please enter a valid integer.")
if __name__ == "__main__":
number_guessing_game()