-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberGuessing.py
52 lines (43 loc) · 1.86 KB
/
NumberGuessing.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import random
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
print("I have selected a random number between 1 and 100. Try to guess it!")
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 7 # Maximum number of attempts allowed
# Provide a hint about the secret number being odd or even
if secret_number % 2 == 0:
print("Hint: The secret number is even.")
else:
print("Hint: The secret number is odd.")
while attempts < max_attempts:
try:
# Get the player's guess
guess = int(input("Enter your guess (between 1 and 100): "))
if guess < 1 or guess > 100:
raise ValueError("Guess out of range")
except ValueError:
print("Invalid input. Please enter a valid number between 1 and 100.")
continue
attempts += 1
# Check if the guess is correct
if guess == secret_number:
print(f"Congratulations! You guessed the correct number {secret_number} in {attempts} attempts.")
break
elif guess < secret_number:
print("Too low! ")
else:
print("Too high! ")
# Provide additional hints based on the difference between the guess and the secret number
difference = abs(secret_number - guess)
if difference >= 20:
print("Hint: You are very far from the secret number.")
elif difference >= 10:
print("Hint: You are getting closer to the secret number.")
else:
print("Hint: You are very close to the secret number.")
if attempts >= max_attempts:
print(f"Sorry, you've run out of attempts. The correct number was {secret_number}.")
if __name__ == "__main__":
number_guessing_game()