-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
72 lines (49 loc) · 1.42 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from random import randrange
def main():
level = get_game_level()
# Level's answer
answer = randrange(level + 1)
# Loop until user wins
while True:
# Store users guess
guess = get_user_guess()
# Store and print result for each guess
result = check_game(guess, answer)
print(result)
# End game if user wins
if result != "Just right!":
continue
else:
break
def get_game_level() -> int:
# Loop until user inputs a positive integer
while True:
try:
level = int(input("Level: "))
# Check for positive integer
if level > 0:
return level
# Retry loop if input is not an integer
except ValueError:
continue
def get_user_guess() -> int:
# Loop until user inputs a positive integer
while True:
try:
guess = int(input("Guess: "))
# Check for positive integer
if 0 < guess:
return guess
# Retry loop if input is not an integer
except ValueError:
pass
def check_game(guess: int, answer: int) -> str:
# Check if guess is less, more, or equal to the answer
if guess < answer:
return "Too small!"
elif guess > answer:
return "Too large!"
else:
return "Just right!"
if __name__ == "__main__":
main()