python 教程 第十四章、 地址薄作业
2013-07-23 17:03
513 查看
第十四章、 地址薄作业
#A Byte of Python #!/usr/bin/env python import cPickle import os #define the contacts file, list global file_contacts global list_contacts file_contacts = 'AddressBook.txt' list_contacts = [] #delete the file try: file_path = os.getcwd() + os.sep + file_contacts if os.path.isfile(file_path): os.remove(file_contacts) f = file(file_contacts,'w') f.close() #define the class of contacts and implement the methods class class_contacts: def __init__(self, list_contacts): self.contacts = list_contacts def append(self, name, mail): dict_contacts = {'Name': name, 'Mail': mail} self.contacts.append(dict_contacts) def find(self, name): found = False for instance_contacts in list_contacts: if name == instance_contacts['Name']: found = True print " Found:", name, ", PASS" break if not found: print " Found:", name, ", FAIL" def modify(self, name, mail): for instance_contacts in list_contacts: if name == instance_contacts['Name']: instance_contacts['Mail'] = mail break def delete(self, name): index = -1 for instance_contacts in list_contacts: index += 1 if name == instance_contacts['Name']: del list_contacts[index] def show(self): for list in list_contacts: print list def save(self): fa = file(file_contacts, "a") cPickle.dump(self.contacts, fa) fa.close() i = class_contacts(list_contacts) i.append('joan', 'joan@123.com') i.append('adny', 'adny@123.com') i.append('xixi', 'xixi@123.com') i.find('joan') i.find('joab') print "Original List:" i.show() print "after modify adny" i.modify('adny', 'adnX@123.com') i.show() print "after del joan" i.delete('joan') i.show() i.save() except TypeError: print "TypeError" except: print "Other Error occured"
相关文章推荐
- python 教程 第十四章、 地址薄作业
- python 教程 第十四章、 地址薄作业
- python 教程 第十四章、 地址薄作业
- Python3-小甲鱼教程 【课后作业及答案】004改进我们的小游戏
- Python学习路线(课程大纲+Python视频教程+下载地址)
- 50余本中外Python电子教程及源码下载地址
- python基础教程 (第十四章)
- 分享一个python cookbook的在线教程地址
- python 视频教程大全下载地址
- selenium+python视频教程地址
- PyCharm 2017 免费 破解 注册 激活 教程(附 License Server 地址)(Python 编辑器 IDE 推荐)
- PyCharm 2017 免费 破解 注册 激活 教程(附 License Server 地址)(Python 编辑器 IDE 推荐)
- Python 视频教程地址
- Python(xy)下载地址、安装教程、使用教程
- Python3-小甲鱼教程 【课后作业及答案】004改进我们的小游戏
- Python 基础教程地址
- python简明教程练习--命令行 地址簿 程序
- Python快速教程博客园地址
- [python3教程]第十四章.交互输入编辑和历史替代(Interactive Input Editing and History Substitution)
- Python-CGI基础教程地址收集