This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
professor.py
59 lines (51 loc) · 1.51 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
48
49
50
51
52
53
54
55
56
57
58
59
import random
def main():
eqn = 10
score = 0
chances = 3
lvl = get_level()
while eqn != 0:
if chances == 3: # User have 3 chances to answer each equation
# Only generate_integer for new equation if chances == 3
x, y = generate_integer(lvl)
try:
user_answer = int(input(f"{x} + {y} = "))
answer = x + y
if user_answer == answer:
eqn = eqn - 1
score = score + 1
chances = 3 # Reset chances to generate new equation in case user input the right answer on 2nd/3rd try
continue
else:
raise ValueError
except (ValueError, NameError):
print("EEE")
chances = chances - 1
pass
if chances == 0:
print((f"{x} + {y} = {answer}"))
chances = 3 # Reset chances to generate new equation
eqn = eqn - 1
continue
print(f"Score: {score}")
def get_level():
while True:
try:
n = int(input("Level: "))
if 1 <= n <= 3:
return n
except:
pass
def generate_integer(level):
if level == 1:
x = random.randint(0, 9)
y = random.randint(0, 9)
elif level == 2:
x = random.randint(10, 99)
y = random.randint(10, 99)
elif level == 3:
x = random.randint(100, 999)
y = random.randint(100, 999)
return x, y
if __name__ == "__main__":
main()