-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathNumber Guessing Game
More file actions
30 lines (25 loc) · 1 KB
/
Copy pathNumber Guessing Game
File metadata and controls
30 lines (25 loc) · 1 KB
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
30
import random
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
lower_limit = 1
upper_limit = 100
secret_number = random.randint(lower_limit, upper_limit)
attempts = 0
while True:
try:
user_guess = int(input(f"Guess the number between {lower_limit} and {upper_limit}: "))
attempts += 1
if user_guess < lower_limit or user_guess > upper_limit:
print(f"Please guess a number between {lower_limit} and {upper_limit}.")
continue
if user_guess < secret_number:
print("Try a higher number.")
elif user_guess > secret_number:
print("Try a lower number.")
else:
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.")
break
except ValueError:
print("Invalid input. Please enter a valid number.")
if __name__ == "__main__":
number_guessing_game()