-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomCalc.py
62 lines (53 loc) · 1.63 KB
/
randomCalc.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
60
61
62
#!/usr/bin/python
# good ideas from https://stackoverflow.com/questions/26260950/how-can-i-randomly-choose-a-maths-operator-and-ask-recurring-maths-questions-wit
import random
import operator
import pprint
import json
def randomCalc():
ops = {'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'/':operator.truediv}
number1 = random.randint(1,10)
number2 = random.randint(1,10) #avoid divide by zero
op = random.choice(list(ops.keys()))
answer = ops.get(op)(number1,number2)
print('What is {} {} {}?\n'.format(number1, op, number2))
return answer
def selectCalc(selected_op):
ops = {'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'/':operator.truediv}
number1 = random.randint(1,10)
number2 = random.randint(1,10) #avoid divide by zero
op = selected_op
question = '{} {} {}'.format(number1, op, number2)
answer = ops.get(op)(number1,number2)
# print('What is {} {} {}?\n'.format(number1, op, number2))
print json.dumps({"Question": question, "Answer": answer})
return answer
def askQuestion():
answer = randomCalc()
guess = float(input())
return guess == answer
def quiz():
print('Welcome. This is a 10 question math quiz\n')
score = 0
for i in range(10):
correct = askQuestion()
if correct:
score += 1
print('Correct!\n')
else:
print('Incorrect!\n')
return 'Your score was {}/10'.format(score)
#print(quiz())
#askQuestion()
#print('Welcome, this is a generated item\n')
#print('Question:\n')
#answer = randomCalc()
# Return 1 question and answer as a json object
answer = selectCalc('+')
#print('Answer: {}'.format(answer))