forked from playerg7/Contribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess a number.py
More file actions
42 lines (32 loc) · 1.38 KB
/
Copy pathGuess a number.py
File metadata and controls
42 lines (32 loc) · 1.38 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
31
32
33
34
35
36
37
38
39
40
41
42
import random
def guess_num():
lower_bound = 1
upper_bound = 50
secret_number = random.randint(lower_bound, upper_bound)
attempts = 0
max_attempts = 12
print("Welcome to the Guess the Number Game!")
print(f"Guess the number between {lower_bound} and {upper_bound} in {max_attempts} chances.")
while attempts < max_attempts:
try:
guess = int(input("Take a guess: "))
if guess < lower_bound or guess > upper_bound:
print(f"Please guess a number between {lower_bound} and {upper_bound}.")
continue
attempts += 1
if guess < secret_number:
print("Too low! Try a higher number.")
elif guess > secret_number:
print("Too high! Try a lower number.")
else:
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.")
break
remaining_attempts = max_attempts - attempts
if remaining_attempts > 0:
print(f"You have {remaining_attempts} attempts remaining.")
except ValueError:
print("Invalid input. Please enter a valid number.")
if attempts >= max_attempts:
print(f"Sorry, you've reached the maximum number of attempts. The secret number was {secret_number}.")
if __name__ == "__main__":
guess_num()