-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.py
More file actions
54 lines (40 loc) · 1.03 KB
/
testing.py
File metadata and controls
54 lines (40 loc) · 1.03 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Number guessing game.
The computer selects a random number and the user
tries to guess it with hints provided.
"""
import random
def get_user_guess() -> int:
"""
Prompt the user for a numeric guess.
:return: User's guessed number
"""
while True:
try:
return int(input("Enter your guess (1–100): "))
except ValueError:
print("Please enter a valid number.")
def play_game() -> None:
"""
Run the guessing game logic.
"""
secret_number = random.randint(1, 100)
attempts = 0
print("🎲 I have selected a number between 1 and 100.")
while True:
guess = get_user_guess()
attempts += 1
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print(f"🎉 Correct! You guessed it in {attempts} attempts.")
break
def main() -> None:
"""
Main entry point.
"""
play_game()
if __name__ == "__main__":
main()