-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
34 lines (28 loc) · 1 KB
/
game.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
33
34
from random import randint
def get_valid_input(prompt, error_message, condition):
while True:
try:
value = int(input(prompt))
if condition(value):
return value
else:
print(error_message)
except ValueError:
print("Invalid input. Please enter a valid integer.")
def main():
level = get_valid_input("Enter the level: ", "Level must be greater than 0.", lambda x: x > 0)
secret_number = randint(1, level)
while True:
try:
guess = get_valid_input("Enter your guess: ", "Invalid guess. Please enter a valid integer.", lambda x: True)
if guess < secret_number:
print("Too small!")
elif guess > secret_number:
print("Too large!")
else:
print("Just right!")
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
if __name__ == "__main__":
main()