Python 地址簿程序

A Bite of Python在文章的结尾要求写一个Python地址簿程序。俗话说:光说不练假把式。于是,就写了一个。

# Filename : addressBook.py #At first, we abstract the model. import os import cPickle class Person: def __init__(self,name,email = "",mobile = "",teleNumber = ""): self.name = name self.email = email self.mobile = mobile self.teleNumber = teleNumber addressPath = 'addressBook.data' addressBook = {} if os.path.exists(addressPath): addressFile = file(addressPath) addressBook = cPickle.load(addressFile) isQuit = False print '''Input a command. 'A' or 'a' Add a person 'D' or 'd' Delete a person 'F' or 'f' Find a peron 'M' or 'm' Modify a person 'Q' or 'q' Quit ''' while not isQuit: command = raw_input("/nInput a command:") if(command == 'A' or command == 'a'): print 'Input Person informaton: name email mobile teleNumber' info = raw_input("Person Info:") info = info.split(' ') person = Person(info[0],info[1],info[2],info[3]) if not addressBook.has_key(person.name): addressBook[person.name] = person print "Success" else: print "In AddressBook, there is a same name with",person.name,"." elif(command == 'D' or command == 'd'): print "Input the peroson's name" name = raw_input("Name:") if addressBook.has_key(name): del addressBook[name] print "Success" else: print "No Person is called",name elif(command == 'F' or command == 'f'): print "Input the name of the person which you want find" name = raw_input('Name:') if not addressBook.has_key(name): print "No Person is called",name else: print 'Email:',addressBook[name].email print 'Mobile:',addressBook[name].mobile print 'TeleNumber:',addressBook[name].teleNumber elif(command == 'M' or command == 'm'): print "Input the person's Name" name = raw_input("Name:") if addressBook.has_key(name): if raw_input("Modify email? y/n/n") == 'y': addressBook[name].email = raw_input("Input Email:") if raw_input("Modify mobile? y/n/n") == 'y': addressBook[name].mobile = raw_input("Input mobile:") if raw_input("Modify teleNumber? y/n/n") == 'y': addressBook[name].teleNumber = raw_input("Input teleNumber:") print "Success" else: print "No Person is called",name elif(command == 'Q' or command == 'q'): print 'The program is exit' isQuit = True elif(command == 'L' or command == 'l'): print '****************************************' for name,detail in addressBook.items(): print 'Name:',detail.name print 'Email:',detail.email print 'Mobile:',detail.mobile print 'TeleNumber:',detail.teleNumber print print '****************************************' else: print command,"is not a command" #Write to the file try: addressFile = file(addressPath,'w') cPickle.dump(addressBook,addressFile) finally: addressFile.close()

初学乍练,见谅见谅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值