-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess_num.py
More file actions
27 lines (22 loc) · 779 Bytes
/
guess_num.py
File metadata and controls
27 lines (22 loc) · 779 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
import random
def guess_number():
random_no = random.randint(1, 100)
attempts = 0
guessed = False
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
while not guessed:
try:
guess = int(input("Take a guess: "))
attempts += 1
if guess < random_no:
print("Too low! Try again.")
elif guess > random_no:
print("Too high! Try again.")
else:
print(f"You got it! The number was {random_no}.")
print(f"It took you {attempts} attempts.")
guessed = True
except ValueError:
print("Please enter a valid number.")
guess_number()