#
database.pyimport sys,shelve
def store_person(db):
"""
Query user for data and store it in the shelf object
"""
pid = raw_input('Enter unique ID number:')
person = {}
person['name']=raw_input('Enter name:')
person['age']=raw_input('Enter age:')
person['phone']=raw_input('Enter phone:')
db[pid]=person
def lookup_person(db):
"""
Query user for ID and desired filed,and fecth the corresponding data form the shelf object
"""
pid = raw_input('Enter ID number')
filed = raw_input('What would you like to know?(name,age,phone)')
filed = filed.strip().lower()
print filed.capitalize() + ':',db[pid][filed]
def print_help():
print'The availabel command are:'
print'store: Store information about a person'
print'lookup: Lookup a person form a ID number'
print 'quit: Save changes and exit'
print'? : prints this message '
def enter_command():
cmd = raw_input('Enter command(? for help):')
cmd = cmd.strip().lower()
return cmd
def main():
database = shelve.open('C:\\database.dat ')
try:
while Ture:
cmd = enter_command()
if cmd =='store':
store_person(database)
elif cmd =='lookup':
lookup_person(database)
elif cmd =='?':
print_help
elif cmd =='quit':
return
finally:
database.close()
if __name__ == '__main__':
main()
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/149046
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.