python 函数版联系人管理系统

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 Python 名片管理系统面向对象的示例代码: ```python class Contact: '''定义联系人类''' def __init__(self, name, phone, email): self.name = name self.phone = phone self.email = email class ContactList: '''定义联系人列表类''' def __init__(self): self.contacts = [] def add_contact(self, contact): '''添加联系人''' self.contacts.append(contact) def remove_contact(self, contact): '''删除联系人''' self.contacts.remove(contact) def search_contact(self, name): '''查找联系人''' for contact in self.contacts: if contact.name == name: return contact return None def display_contacts(self): '''显示所有联系人''' for contact in self.contacts: print('Name:', contact.name) print('Phone:', contact.phone) print('Email:', contact.email) print() def main(): '''程序入口''' contact_list = ContactList() while True: print('1. Add Contact') print('2. Remove Contact') print('3. Search Contact') print('4. Display Contacts') print('5. Exit') choice = input('Enter your choice: ') if choice == '1': name = input('Enter name: ') phone = input('Enter phone: ') email = input('Enter email: ') contact = Contact(name, phone, email) contact_list.add_contact(contact) print('Contact added successfully.') elif choice == '2': name = input('Enter name: ') contact = contact_list.search_contact(name) if contact: contact_list.remove_contact(contact) print('Contact removed successfully.') else: print('Contact not found.') elif choice == '3': name = input('Enter name: ') contact = contact_list.search_contact(name) if contact: print('Name:', contact.name) print('Phone:', contact.phone) print('Email:', contact.email) else: print('Contact not found.') elif choice == '4': contact_list.display_contacts() elif choice == '5': break else: print('Invalid choice. Try again.') if __name__ == '__main__': main() ``` 这个程序包含两个类:Contact 和 ContactList。Contact 类表示一个联系人,包含三个属性:姓名、电话和电子邮件。ContactList 类表示联系人列表,包含一个列表属性 contacts,以及添加、删除、查找和显示联系人的方法。程序入口在 main 函数中,通过循环读取用户输入,根据用户选择调用不同的方法实现不同的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值