-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_yourself.py
86 lines (56 loc) · 1.43 KB
/
test_yourself.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
import sys
import random
import MySQLdb
db = MySQLdb.connect(user='nick', db='wikipedia')
c = db.cursor()
try:
print "game type?"
choice = sys.stdin.readline().strip()
if choice == '1':
print "what number is this word?"
print
while 1:
c.execute("select number, word from pro order by rand() limit 1")
(number, word) = c.fetchone()
print word, '?'
while 1:
input = sys.stdin.readline()
if not input.strip():
print "correct was", number
print
break
try:
if int(input.strip()) == int(number):
print "correct"
print
break
else:
raise ValueError
except ValueError:
print "incorrect, try again"
elif choice == '2':
while 1:
num = str(random.randint(0, 999))
c.execute("select count(*) from pro where number=%s", num)
if c.fetchone()[0] == 0:
continue
print "pick a word for this number:", num
while 1:
input = sys.stdin.readline().strip()
if not input:
c.execute("select word from pro where number=%s", num)
print 'correct could have been:', ', '.join(x[0] for x in c.fetchall())
break
c.execute("select count(*) from pro where word=%s and number=%s", (input, num))
cnt = c.fetchone()[0]
if cnt:
print "correct"
print
break
else:
print "incorrect, try again"
else:
print "unknown game type"
print
except (SystemExit, KeyboardInterrupt):
print "exiting..."