-
Notifications
You must be signed in to change notification settings - Fork 0
/
professor.py
47 lines (41 loc) · 1.06 KB
/
professor.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
from random import randint
def main():
level = get_level()
score = play_game(level)
print("Score:", score)
def get_level():
while True:
try:
level = int(input("Level: "))
if level in [1, 2, 3]:
return level
except ValueError:
pass
def generate_integer(level):
if level == 1:
return randint(0, 9)
return randint(10 ** (level - 1), 10**level - 1)
def check_answer(x, y):
tries = 0
while tries < 3:
tries += 1
try:
answer = int(input(f"{x} + {y} = "))
if answer == x + y:
print("Correct!")
return 1
else:
print("EEE")
except ValueError:
print("Invalid input. Please enter a number.")
print(f"{x} + {y} = {x + y}")
return 0
def play_game(level):
score = 0
for _ in range(10):
x = generate_integer(level)
y = generate_integer(level)
score += check_answer(x, y)
return score
if __name__ == "__main__":
main()