-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_game.py
46 lines (32 loc) · 907 Bytes
/
math_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
#!/usr/bin/env python3
#Philip Hansen 2015
import random
def gen_math():
signs = ["+", "-"]
number_1 = random.randint(0, 10)
number_2 = random.randint(0, 10)
sign = signs[random.randint(0, 1)]
question = (str(number_1) + sign + str(number_2))
answer = eval(question)
return question, answer
def quiz():
global points
question, answer = gen_math()
print("Your question is: " + question)
given_answer = int(input("Answer: "))
if given_answer == answer:
points += 1
print("Correct!")
else:
print("Wrong, the correct answer was " + str(answer))
def main():
global points
points = 0
rounds = 10
for i in range(rounds):
quiz()
#Once the for loop is done
else:
print("You completed the game with " + str(points) + "/" + str(rounds) + " points.")
if __name__ == "__main__":
main()