-
Notifications
You must be signed in to change notification settings - Fork 0
/
ask_rquestion.py
executable file
·113 lines (90 loc) · 2.81 KB
/
ask_rquestion.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
import os, sys, shelve, random
from pprint import pprint
show_right_ans = False # print right answer if user got it wrong
qfname = "questions.db"
questions = []
def load_questions():
qfp = shelve.open(qfname)
for key in qfp.keys():
for question in qfp[key]:
questions.append((key, question))
def ask_text(question):
""" Ask a question, return true or false.
Question looks like: (question, ((answer,0), (answer,1),) ...) where 1
means the right answer.
Return question text and the number of the right answer & text string of
right answer.
"""
random.shuffle(question[1])
ans = question[1]
text = ""
text += question[0][1] + "\n"
for a in range(len(ans)):
text += "%d) %s\n" % (a+1,ans[a][0])
if ans[a][1] == 1:
right_num = a+1
right_answer = ans[a][0]
text += "> \n"
return (text, right_num, right_answer)
def ask(question):
""" Ask a question, return true or false.
Question looks like: (question, ((answer,0), (answer,1),) ...) where 1
means the right answer.
Returns is_right, right_ans # where is_right is 0 or 1, and right_ans is a
string.
"""
random.shuffle(question[1])
ans = question[1]
while True:
print (question[0][1])
right_ans = None
for a in range(len(ans)):
print ("%d) %s" % (a+1,ans[a][0]))
if ans[a][1]:
right_ans = ans[a][0]
answer = raw_input("=====> ")
if answer == "q":
sys.exit()
try:
answer = int(answer)
except:
continue
if answer > len(ans) or answer < 1:
continue
return ans[answer-1][1], right_ans
def test_ask_text():
q = ["In low-level light conditions, the eyes of Tawny Owl are:",
[["50 times better than human eyes.", 0],
["100 times better than human eyes.", 1],
["200 times better than human eyes.", 0],
["300 times better than human eyes.", 0],]]
tmp = ask_text(q)
assert tmp[2] == "100 times better than human eyes."
print ("Tests passed!")
def tests():
test_ask_text()
def main():
load_questions()
pprint(questions[0])
tests()
q = random.choice(questions)[1]
text, right_num, right_ans = ask_text(q)
print (text)
print ("Right answer:", right_ans)
print ("\n\n")
while True:
q = random.choice(questions)[1]
result, right_ans = ask(q)
if result:
print("!!!Right answer!!!!")
print()
else:
print("...Wrong answer...",)
if show_right_ans:
print("(%s)" % right_ans)
else:
print()
print()
if __name__ == "__main__":
main()