1 #! /usr/bin/env python 2 #coding=utf-8 3 import sys,shelve 4 def store_person(db): 5 pid=raw_input(‘Enter unique ID number:‘) 6 person={} 7 person[‘name‘]=raw_input(‘Enter name:‘) 8 person[‘age‘]=raw_input(‘Enter age:‘) 9 person[‘phone‘]=raw_input(‘Enter phone number:‘) 10 db[pid]=person# 11 def lookup_person(db): 12 pid=raw_input(‘Enter ID number:‘) 13 field=raw_input(‘What would you like to know?(name,age,phone)‘) 14 field=field.strip().lower()# 15 print field.capitalize()+‘:‘,db[pid][field]# 16 def print_help(): 17 print‘The available commands are:‘ 18 print‘store:Stores information about a person‘ 19 print‘lookup:Looks up a person from ID number‘ 20 print‘quit:Save changes and exit‘ 21 print‘? :Prints this messages‘ 22 def enter_command(): 23 cmd=raw_input(‘Enter command(? for help):‘) 24 cmd=cmd.strip().lower() 25 return cmd 26 def main(): 27 database=shelve.open(‘C://database.dat‘) 28 try: 29 while True: 30 cmd=enter_command() 31 if cmd==‘store‘: 32 store_person(database) 33 elif cmd==‘lookup‘: 34 lookup_person(database) 35 elif cmd==‘?‘: 36 print_help() 37 elif cmd==‘quit‘: 38 return 39 finally: 40 database.close() 41 if __name__==‘__main__‘:main()
时间: 2024-10-26 02:39:00