本项目主要结合教材所学知识开发一个个人版通讯录,要求通讯录可以实现以下功能: 添加联系人 删除联系人 查找联系人 联系人列表 通讯录存盘 读出通讯录 退出
时间: 2024-05-04 18:16:54 浏览: 17
好的,这是一个很好的练手项目,可以加深对于Python基础知识的理解和运用。你可以按照以下步骤来完成这个通讯录的开发:
1. 定义 Contact 类,该类包含联系人的姓名、电话号码和邮箱等信息。
2. 定义 AddressBook 类,该类包含添加联系人、删除联系人、查找联系人、联系人列表、通讯录存盘、读出通讯录等方法。
3. 通过文件 I/O 实现通讯录的存储和读取。
4. 使用命令行界面实现交互式操作,即用户输入命令后,程序根据用户输入的命令来执行相应的功能。
以下是示例代码,供你参考:
```python
import pickle
class Contact:
def __init__(self, name, phone_number, email):
self.name = name
self.phone_number = phone_number
self.email = email
class AddressBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
def remove_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
return True
return False
def find_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None
def get_contacts(self):
return self.contacts
def save_to_file(self, filename):
with open(filename, 'wb') as f:
pickle.dump(self.contacts, f)
def load_from_file(self, filename):
with open(filename, 'rb') as f:
self.contacts = pickle.load(f)
def main():
address_book = AddressBook()
while True:
print('请输入命令:')
print('add: 添加联系人')
print('remove: 删除联系人')
print('find: 查找联系人')
print('list: 联系人列表')
print('save: 通讯录存盘')
print('load: 读出通讯录')
print('quit: 退出')
command = input()
if command == 'add':
name = input('请输入联系人姓名: ')
phone_number = input('请输入联系人电话号码: ')
email = input('请输入联系人邮箱: ')
contact = Contact(name, phone_number, email)
address_book.add_contact(contact)
print('联系人添加成功!')
elif command == 'remove':
name = input('请输入联系人姓名: ')
if address_book.remove_contact(name):
print('联系人删除成功!')
else:
print('联系人不存在!')
elif command == 'find':
name = input('请输入联系人姓名: ')
contact = address_book.find_contact(name)
if contact:
print('联系人信息:')
print('姓名:', contact.name)
print('电话号码:', contact.phone_number)
print('邮箱:', contact.email)
else:
print('联系人不存在!')
elif command == 'list':
contacts = address_book.get_contacts()
print('联系人列表:')
for contact in contacts:
print(contact.name)
elif command == 'save':
filename = input('请输入文件名: ')
address_book.save_to_file(filename)
print('通讯录存盘成功!')
elif command == 'load':
filename = input('请输入文件名: ')
address_book.load_from_file(filename)
print('通讯录读取成功!')
elif command == 'quit':
break
if __name__ == '__main__':
main()
```
注意:在运行程序之前,需要先创建一个空文件作为通讯录存储文件,程序中默认文件名为 'address_book.dat'。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)