|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pickle as p
- #打开存储文件
- fileName = 'Contact_Information.txt'
- with open(fileName,'wb+') as f:
- #把存储信息的字典弄进去
- contactDict = {'example':'19890604590'}
- p.dump(contactDict,f)
- #加载文件
- p.load(f)
- #定义一个存储信息的类
- class Information:
- #添加联系人
- def addContact(self,name,contact):
- self.name = name
- self.contact = contact
- if name in contactDict:
- temp = input('联系人已存在,是否要修改联系人?(YES/NO):')
- answers1 = ['YES', 'yes', 'Yes']
- answers2 = ['NO', 'no', 'No']
- if temp in answers1:
- contactDict[name] = contact
- p.dump(contactDict, f)
- print('您已成功修改联系人')
- if temp in answers2:
- print('您已取消修改联系人')
- else:
- print('输入有误')
- else:
- contactDict[name] = contact
- f.write(contactDict)
- #修改联系人
- def modifyContact(self,name,contact):
- if name in contactDict:
- temp = input('联系人已存在,是否要修改联系人?(YES/NO):')
- answers1 = ['YES','yes','Yes']
- answers2 = ['NO','no','No']
- if temp in answers1:
- contactDict[name] = contact
- p.dump(contactDict[name],f)
- print('您已成功修改联系人')
- if temp in answers2:
- print('您已取消修改联系人')
- else:
- print('输入有误')
- else:
- print('您输入的联系人不存在')
- #删除联系人
- def deleteContact(self,name):
- if name in contactDict:
- temp = input('是否要删除联系人?(YES/NO):')
- answers1 = ['YES','yes','Yes']
- answers2 = ['NO','no','No']
- if temp in answers1:
- del contactDict[name]
- p.dump(contactDict,f)
- print('您已成功删除联系人')
- if temp in answers2:
- print('您已取消删除联系人')
- else:
- print('输入有误')
- else:
- print('您输入的联系人不存在')
- #搜索联系人
- def sreachContact(self,name):
- if name in contactDict:
- print(name,':',contactDict[name])
- else:
- print('您输入的联系人不存在')
- print('|--- 欢迎进入通讯录程序 ---|\n|--- 1:搜索联系人资料 ---|\n|--- 2:添加新的联系人 ---|\n|--- 3:删除已有联系人 ---|\n|--- 4:修改已有联系人 ---|\n|--- 5:退出通讯录程序 ---|')
- #实例化类
- information = Information()
- while True:
- instr = int(input('\n请输入相关的指令代码:'))
- if instr == 1:
- name1 = input('请输入联系人姓名:')
- information.sreachContact(name1)
- if instr == 2:
- name2 = input('请输入联系人姓名:')
- contact2 = input('请输入联系人联系方式:')
- information.addContact(name2,contact2)
- if instr == 3:
- name3 = input('请输入联系人姓名:')
- information.deleteContact(name3)
- if instr == 4:
- name4 = input('请输入联系人姓名:')
- contact4 = input('请输入联系人联系方式:')
- information.modifyContact(name4,contact4)
- if instr == 5:
- break
- print('|--- 感谢使用通讯录程序 ---|',)
复制代码
- Traceback (most recent call last):
- File "/Users/luoziqian/PycharmProjects/PythonProgram/命令行地址薄程序.py", line 9, in <module>
- p.load(f)
- EOFError: Ran out of input
复制代码
会出现一大堆错误
题目要求:
我会建议你先解决这样一个问题:
创建你自己的命令行地址簿程序。在这个程序中,你可以添加、修改、删除和搜 索你的联系人(朋友、家人和同事等等)以及它们的信息(诸如电子邮件地址和/或 电话号码)。这些详细信息应该被保存下来以便以后提取。
思考一下我们到目前为止所学的各种东西的话,你会觉得这个问题其实相当简 单。如果你仍然希望知道该从何处入手的话,那么这里也有一个提示。 提示(先不要读): 建一个类来表示人的信息。用字典来存储人的对象,可以将名字作为其键值。用 pickle 模块将对 象永久地存在磁盘上。用字典的内置方法实现增加,删除和修改联系人的信息。
大佬快来啊
- import pickle as p
- #打开存储文件
- fileName = 'Contact_Information.txt'
- with open(fileName,'wb+') as f:
- #把存储信息的字典弄进去
- contactDict = {'example':'19890604590'}
- p.dump(contactDict,f)
- f.seek(0) #写入文件后,文件指针指向了最后,如果要继续操作,应该把文件指针
- #指回合适的位置,这里f.seek(0)指回文件头
- #加载文件
- p.load(f)
- #定义一个存储信息的类
- class Information:
- #添加联系人
- def addContact(self,name,contact):
- self.name = name
- self.contact = contact
- if name in contactDict:
- temp = input('联系人已存在,是否要修改联系人?(YES/NO):')
- answers1 = ['YES', 'yes', 'Yes']
- answers2 = ['NO', 'no', 'No']
- if temp in answers1:
- contactDict[name] = contact
- p.dump(contactDict, f)
- print('您已成功修改联系人')
- if temp in answers2:
- print('您已取消修改联系人')
- else:
- print('输入有误')
- else:
- contactDict[name] = contact
- f.write(contactDict)
- #修改联系人
- def modifyContact(self,name,contact):
- if name in contactDict:
- temp = input('联系人已存在,是否要修改联系人?(YES/NO):')
- answers1 = ['YES','yes','Yes']
- answers2 = ['NO','no','No']
- if temp in answers1:
- contactDict[name] = contact
- p.dump(contactDict[name],f)
- print('您已成功修改联系人')
- if temp in answers2:
- print('您已取消修改联系人')
- else:
- print('输入有误')
- else:
- print('您输入的联系人不存在')
- #删除联系人
- def deleteContact(self,name):
- if name in contactDict:
- temp = input('是否要删除联系人?(YES/NO):')
- answers1 = ['YES','yes','Yes']
- answers2 = ['NO','no','No']
- if temp in answers1:
- del contactDict[name]
- p.dump(contactDict,f)
- print('您已成功删除联系人')
- if temp in answers2:
- print('您已取消删除联系人')
- else:
- print('输入有误')
- else:
- print('您输入的联系人不存在')
- #搜索联系人
- def sreachContact(self,name):
- if name in contactDict:
- print(name,':',contactDict[name])
- else:
- print('您输入的联系人不存在')
- print('|--- 欢迎进入通讯录程序 ---|\n|--- 1:搜索联系人资料 ---|\n|--- 2:添加新的联系人 ---|\n|--- 3:删除已有联系人 ---|\n|--- 4:修改已有联系人 ---|\n|--- 5:退出通讯录程序 ---|')
- #实例化类
- information = Information()
- while True:
- instr = int(input('\n请输入相关的指令代码:'))
- if instr == 1:
- name1 = input('请输入联系人姓名:')
- information.sreachContact(name1)
- if instr == 2:
- name2 = input('请输入联系人姓名:')
- contact2 = input('请输入联系人联系方式:')
- information.addContact(name2,contact2)
- if instr == 3:
- name3 = input('请输入联系人姓名:')
- information.deleteContact(name3)
- if instr == 4:
- name4 = input('请输入联系人姓名:')
- contact4 = input('请输入联系人联系方式:')
- information.modifyContact(name4,contact4)
- if instr == 5:
- break
- print('|--- 感谢使用通讯录程序 ---|',)
复制代码
|
|