2. 编写通讯录管理程序。定义一个Friend类,包括name(姓名)、sex(性别)、age(年龄)、phone(电话)、mail(邮箱)、qq(QQ号码)、company(单位)等属性和对这些属性的操作函数,再定义通讯录类(Addressbook),实现通讯录列表、查询、修改、添加、删除、保存、退出等功能。
时间: 2023-07-10 15:43:37 浏览: 106
好的,我可以为您提供一些关于通讯录管理程序的思路和代码示例。
首先,我们定义一个Friend类,包括name(姓名)、sex(性别)、age(年龄)、phone(电话)、mail(邮箱)、qq(QQ号码)、company(单位)等属性和对这些属性的操作函数,示例代码如下:
```python
class Friend:
def __init__(self, name, sex, age, phone, mail, qq, company):
self.name = name
self.sex = sex
self.age = age
self.phone = phone
self.mail = mail
self.qq = qq
self.company = company
def edit_name(self, name):
self.name = name
def edit_sex(self, sex):
self.sex = sex
def edit_age(self, age):
self.age = age
def edit_phone(self, phone):
self.phone = phone
def edit_mail(self, mail):
self.mail = mail
def edit_qq(self, qq):
self.qq = qq
def edit_company(self, company):
self.company = company
```
接下来,我们定义通讯录类(Addressbook),实现通讯录列表、查询、修改、添加、删除、保存、退出等功能。示例代码如下:
```python
class Addressbook:
def __init__(self):
self.friend_list = []
def add_friend(self, friend):
self.friend_list.append(friend)
def delete_friend(self, name):
for friend in self.friend_list:
if friend.name == name:
self.friend_list.remove(friend)
print(f"已删除{name}的信息!")
break
else:
print(f"通讯录中没有{name}的信息!")
def modify_friend(self, name):
for friend in self.friend_list:
if friend.name == name:
print(f"{name}的信息为:")
print(f"姓名:{friend.name} 性别:{friend.sex} 年龄:{friend.age} 电话:{friend.phone} 邮箱:{friend.mail} QQ号码:{friend.qq} 单位:{friend.company}")
print("请输入要修改的信息:")
friend.edit_name(input("姓名:"))
friend.edit_sex(input("性别:"))
friend.edit_age(input("年龄:"))
friend.edit_phone(input("电话:"))
friend.edit_mail(input("邮箱:"))
friend.edit_qq(input("QQ号码:"))
friend.edit_company(input("单位:"))
print(f"已成功修改{name}的信息!")
break
else:
print(f"通讯录中没有{name}的信息!")
def view_friend_list(self):
print("通讯录列表:")
for friend in self.friend_list:
print(f"姓名:{friend.name} 性别:{friend.sex} 年龄:{friend.age} 电话:{friend.phone} 邮箱:{friend.mail} QQ号码:{friend.qq} 单位:{friend.company}")
def query_friend(self, name):
for friend in self.friend_list:
if friend.name == name:
print(f"姓名:{friend.name} 性别:{friend.sex} 年龄:{friend.age} 电话:{friend.phone} 邮箱:{friend.mail} QQ号码:{friend.qq} 单位:{friend.company}")
break
else:
print(f"通讯录中没有{name}的信息!")
def save_to_file(self):
with open("addressbook.txt", "w", encoding="utf-8") as f:
for friend in self.friend_list:
f.write(f"{friend.name},{friend.sex},{friend.age},{friend.phone},{friend.mail},{friend.qq},{friend.company}\n")
def load_from_file(self):
try:
with open("addressbook.txt", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(",")
friend = Friend(data[0], data[1], data[2], data[3], data[4], data[5], data[6])
self.friend_list.append(friend)
print("已成功从文件中加载通讯录!")
except FileNotFoundError:
print("通讯录文件不存在!")
def run(self):
self.load_from_file()
while True:
print("1.查看通讯录列表 2.查询好友信息 3.添加好友信息 4.修改好友信息 5.删除好友信息 6.保存通讯录 7.退出程序")
choice = input("请输入您的选择:")
if choice == "1":
self.view_friend_list()
elif choice == "2":
name = input("请输入要查询的姓名:")
self.query_friend(name)
elif choice == "3":
name = input("请输入姓名:")
sex = input("请输入性别:")
age = input("请输入年龄:")
phone = input("请输入电话:")
mail = input("请输入邮箱:")
qq = input("请输入QQ号码:")
company = input("请输入单位:")
friend = Friend(name, sex, age, phone, mail, qq, company)
self.add_friend(friend)
print(f"已成功添加{name}的信息!")
elif choice == "4":
name = input("请输入要修改的姓名:")
self.modify_friend(name)
elif choice == "5":
name = input("请输入要删除的姓名:")
self.delete_friend(name)
elif choice == "6":
self.save_to_file()
print("已成功保存通讯录!")
elif choice == "7":
print("感谢使用本程序!")
break
else:
print("输入错误,请重新输入!")
```
以上就是一个简单的通讯录管理程序的示例代码,您可以根据自己的需求进行修改和完善。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)