本文提供一个Python通信录程序的基本框架和思路,包括添加联系人、删除联系人、修改联系人信息、查找联系人、显示所有联系人等功能。

首先,我们需要定义一个通信录类,它应该包括以下功能:

  1. 添加联系人
  2. 删除联系人
  3. 修改联系人信息
  4. 查找联系人
  5. 显示所有联系人

接着,我们可以定义一个联系人类,它应该包括以下属性:

  1. 姓名
  2. 电话号码
  3. 邮箱地址
  4. 地址

然后,我们可以使用一个列表来存储所有联系人,并在通信录类中实现上述功能。例如:

class Contact:
    def __init__(self, name, phone, email, address):
        self.name = name
        self.phone = phone
        self.email = email
        self.address = address

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)
                break

    def modify_contact(self, name, phone=None, email=None, address=None):
        for contact in self.contacts:
            if contact.name == name:
                if phone:
                    contact.phone = phone
                if email:
                    contact.email = email
                if address:
                    contact.address = address
                break

    def search_contact(self, name):
        for contact in self.contacts:
            if contact.name == name:
                return contact
        return None

    def display_all_contacts(self):
        for contact in self.contacts:
            print(f'Name: {contact.name}
Phone: {contact.phone}
Email: {contact.email}
Address: {contact.address}
')

最后,我们可以在程序入口处实现一个简单的菜单,让用户可以选择相应的功能。例如:

if __name__ == '__main__':
    address_book = AddressBook()

    while True:
        print('Address Book')
        print('1. Add Contact')
        print('2. Remove Contact')
        print('3. Modify Contact')
        print('4. Search Contact')
        print('5. Display All Contacts')
        print('6. Quit')

        choice = input('Enter your choice: ')

        if choice == '1':
            name = input('Enter name: ')
            phone = input('Enter phone number: ')
            email = input('Enter email address: ')
            address = input('Enter address: ')
            contact = Contact(name, phone, email, address)
            address_book.add_contact(contact)
        elif choice == '2':
            name = input('Enter name: ')
            address_book.remove_contact(name)
        elif choice == '3':
            name = input('Enter name: ')
            phone = input('Enter new phone number (press enter to skip): ')
            email = input('Enter new email address (press enter to skip): ')
            address = input('Enter new address (press enter to skip): ')
            address_book.modify_contact(name, phone, email, address)
        elif choice == '4':
            name = input('Enter name: ')
            contact = address_book.search_contact(name)
            if contact:
                print(f'Name: {contact.name}
Phone: {contact.phone}
Email: {contact.email}
Address: {contact.address}
')
            else:
                print('Contact not found')
        elif choice == '5':
            address_book.display_all_contacts()
        elif choice == '6':
            break
        else:
            print('Invalid choice')

这是一个简单的通信录程序,你可以根据自己的需求进行修改和扩展。

Python 通信录程序开发教程:基本框架和思路

原文地址: https://www.cveoy.top/t/topic/nFfU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录