/
speedmath.py
58 lines (48 loc) · 1.21 KB
/
speedmath.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
import time
import random
def start_game():
difficulty = 0
maximum = 9
while True:
level = raw_input("Ready to play Speed Math? What level? [ 1 (easy), 2 (medium), or 3 (hard) ] ")
if int(level) < 4 and int(level > 0):
difficulty = int(level)
break
else:
print "Please choose a number between 1 and 3, with 3 being the most difficult."
maximum = get_maximum(difficulty)
ready = raw_input("Press enter to begin. ")
score = 0
start = time.time()
print 'START'
operands = ['+','-','*']
for i in range(10):
a = random.randint(0,maximum)
b = random.randint(0,maximum)
op = operands[random.randint(0,2)]
if (op == '+'):
ans = a + b
guess = raw_input(str(a) + ' + ' + str(b) + ' = ')
if guess == str(ans):
score += 1
elif (op == '-'):
ans = a - b
guess = raw_input(str(a) + ' - ' + str(b) + ' = ')
if guess == str(ans):
score += 1
else:
ans = a * b
guess = raw_input(str(a) + ' * ' + str(b) + ' = ')
if guess == str(ans):
score += 1
end = time.time()
print 'Time = ' + str(int(end - start))
print 'Score = ' + str(score) + ' / 10'
def get_maximum(diff):
if (diff == 1):
return 9
elif (diff == 2):
return 20
else:
return 50
start_game()