forked from CSEdgeOfficial/Python-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTASK3.py
33 lines (26 loc) · 1010 Bytes
/
TASK3.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
import random
def guess_number():
# 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("I have chosen a number between 1 and 100. You have", max_attempts, "attempts to guess it.")
while attempts < max_attempts:
try:
guess = int(input("Enter your guess: "))
except ValueError:
print("Invalid input! Please enter a valid number.")
continue
attempts += 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print("Congratulations! You've guessed the number", secret_number, "correctly in", attempts, "attempts!")
break
else:
print("Sorry, you've run out of attempts. The correct number was", secret_number)
if __name__ == "__main__":
guess_number()