-
Notifications
You must be signed in to change notification settings - Fork 0
/
use.py
51 lines (33 loc) · 906 Bytes
/
use.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
import sys
import MySQLdb
db = MySQLdb.connect(user='nick', db='wikipedia')
c = db.cursor()
print "how may i help you?"
print "1) look up a word for a number"
print "2) look up a number for a word"
try:
choice = sys.stdin.readline().strip()
if choice == '1':
while True:
print "number:"
number = sys.stdin.readline().strip()
try:
int(number)
except ValueError:
print "that's not a number, is it?"
else:
num_found = c.execute("select word from pro where number=%s", number)
print "found", num_found, ", ".join(x[0] for x in c.fetchall())
elif choice == '2':
while True:
print "word:"
word = sys.stdin.readline().strip()
num_found = c.execute("select number from pro where word=%s", word)
if not num_found:
print "none found"
else:
print c.fetchone()[0]
else:
print "unknown choice"
except KeyboardInterrupt:
print "exiting"