forked from Sbiswas001/Basic-python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_guessing_game.py
More file actions
38 lines (30 loc) · 1.18 KB
/
number_guessing_game.py
File metadata and controls
38 lines (30 loc) · 1.18 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
import random
def play_number_guessing_game():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 10
print("Welcome to the Number Guessing Game!")
print(f"I'm thinking of a number between 1 and 100.")
print(f"You have {max_attempts} attempts to guess it.")
while attempts < max_attempts:
try:
# Get user's guess
guess = int(input("\nEnter your guess: "))
attempts += 1
# Check if guess is correct
if guess == secret_number:
print(f"\nCongratulations! You've guessed the number in {attempts} attempts!")
return
elif guess < secret_number:
print("Too low! Try a higher number.")
else:
print("Too high! Try a lower number.")
# Show remaining attempts
print(f"Attempts remaining: {max_attempts - attempts}")
except ValueError:
print("Please enter a valid number!")
continue
print(f"\nGame Over! The number was {secret_number}.")
if __name__ == "__main__":
play_number_guessing_game()