-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathGuessing_game.py
34 lines (29 loc) · 1.3 KB
/
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
30
31
32
33
34
import random
def guess_the_number():
# Set the range for the random number
lower_bound = 1
upper_bound = 100
# Generate a random number
random_number = random.randint(lower_bound, upper_bound)
# Set the number of attempts
attempts = 7
print(f"Guess the number between {lower_bound} and {upper_bound}. You have {attempts} attempts.")
for attempt in range(1, attempts + 1):
try:
guess = int(input(f"Attempt {attempt}: Enter your guess: "))
if guess < lower_bound or guess > upper_bound:
print(f"Please guess a number between {lower_bound} and {upper_bound}.")
continue
# Check if the guess is correct
if guess == random_number:
print(f"Congratulations! You guessed the correct number {random_number} in {attempt} attempts!")
break
elif guess < random_number:
print("Too low! Try a higher number.")
else:
print("Too high! Try a lower number.")
except ValueError:
print("Please enter a valid number.")
if attempt == attempts:
print(f"Sorry, you've used all {attempts} attempts. The correct number was {random_number}. Better luck next time!")
guess_the_number()