-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessNo.py
More file actions
30 lines (24 loc) · 880 Bytes
/
Copy pathGuessNo.py
File metadata and controls
30 lines (24 loc) · 880 Bytes
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
//Number Guessing Game
import random
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
# Generate a random number between 1 and 100
number_to_guess = random.randint(1, 100)
# Initialize the number of attempts
attempts = 0
while True:
try:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < number_to_guess:
print("Too low. Try again.")
elif guess > number_to_guess:
print("Too high. Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
except ValueError:
print("Please enter a valid integer.")
# Run the game
number_guessing_game()