-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_revision.py
46 lines (33 loc) · 931 Bytes
/
game_revision.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
from random import randint
def main():
# Loop till user wins
while True:
try:
# Get level
level = get_positive_integer("Level: ")
# Generate random answer based on level
answer = randint(1, level)
# Start guessing game
game(answer)
# Catch invalid inputs
except (ValueError, Exception):
pass
def get_positive_integer(prompt) -> int:
level = int(input(prompt))
if level > 0:
return level
# Non-positive integer
raise Exception
def game(answer: int) -> str:
while True:
guess = int(input("Guess: "))
# Feedback for each guess to answer
if guess > answer:
print("Too large!")
elif guess < answer:
print("Too small!")
else:
print("Just right!")
quit()
if __name__ == "__main__":
main()