forked from foxli180/BeginningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch10-p189-database.py
44 lines (33 loc) · 1.07 KB
/
ch10-p189-database.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
import sys,shelve
def store_person(db):
pid = input('Enter unique ID number:')
person = {}
person['name'] = input('Enter name:')
person['age'] = input('Enter age:')
person['phone'] = input('Phone number:')
db[pid] = person
def lookup_person(db):
pid = input('Enter ID number:')
field = input('What would u like to know?(name,age,phone)')
field = field.strip().lower()
print (field.capitalize()+':'+db[pid][field])
def enter_command():
cmd = input('Enter command(? for help)')
cmd = cmd.strip().lower()
return cmd
def main():
database = shelve.open('c:/1/database.dat')
try:
while True:
cmd = enter_command()
if cmd == 'store':
store_person(database)
elif cmd == 'lookup':
lookup_person(database)
elif cmd == '?':
pass
elif cmd == 'quit':
return
finally:
database.close()
if __name__ == '__main__':main()