forked from KALIIOWORK/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
29 lines (22 loc) · 816 Bytes
/
Copy pathnumber_guessing_game.py
File metadata and controls
29 lines (22 loc) · 816 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
import random
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
secret_number = random.randint(1, 100)
attempts = 0
while True:
user_guess = input("Enter your guess: ")
attempts += 1
if not user_guess.isdigit():
print("Please enter a valid number.")
continue
user_guess = int(user_guess)
if user_guess < secret_number:
print("Too low! Try again.")
elif user_guess > secret_number:
print("Too high! Try again.")
else:
print(f"Correct! Congratulations, you guessed the number in {attempts} attempts!")
break
if __name__ == "__main__":
number_guessing_game()