forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
29 lines (22 loc) · 780 Bytes
/
script.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
import random
def guess_the_number():
print("Welcome to Guess the Number Game!")
print("I'm thinking of a number between 1 and 100.")
secret_number = random.randint(1, 100)
attempts = 0
while True:
try:
guess = int(input("Enter your guess: "))
attempts += 1
if guess == secret_number:
print(
f"Congratulations! You guessed the number in {attempts} attempts.")
break
elif guess < secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
if __name__ == "__main__":
guess_the_number()