117.info
人生若只如初见

python通讯录程序代码怎么写

下面是一个简单的Python通讯录程序代码示例:

class Contact:
    def __init__(self, name, phone_number):
        self.name = name
        self.phone_number = phone_number

class AddressBook:
    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):
        if len(self.contacts) == 0:
            print("No contacts found.")
        else:
            for contact in self.contacts:
                print(f"Name: {contact.name}, Phone Number: {contact.phone_number}")

def main():
    address_book = AddressBook()

    while True:
        print("1. Add Contact")
        print("2. Remove Contact")
        print("3. Search Contact")
        print("4. Display All Contacts")
        print("5. Quit")

        choice = input("Enter your choice: ")

        if choice == "1":
            name = input("Enter name: ")
            phone_number = input("Enter phone number: ")
            contact = Contact(name, phone_number)
            address_book.add_contact(contact)
            print("Contact added.")
        elif choice == "2":
            name = input("Enter name: ")
            contact = address_book.search_contact(name)
            if contact:
                address_book.remove_contact(contact)
                print("Contact removed.")
            else:
                print("Contact not found.")
        elif choice == "3":
            name = input("Enter name: ")
            contact = address_book.search_contact(name)
            if contact:
                print(f"Name: {contact.name}, Phone Number: {contact.phone_number}")
            else:
                print("Contact not found.")
        elif choice == "4":
            address_book.display_contacts()
        elif choice == "5":
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

这个程序通过创建Contact类和AddressBook类来实现通讯录的功能。Contact类用于表示一个联系人,包含姓名和电话号码两个属性。AddressBook类用于表示通讯录,包含一个联系人列表,以及添加、删除、搜索和显示联系人的方法。

main函数中,通过一个无限循环,根据用户的选择执行相应的操作,包括添加联系人、删除联系人、搜索联系人、显示所有联系人以及退出程序。

注意:上述代码只是一个简单的示例,实际的通讯录程序可能需要更多的功能和优化。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe2d4AzsLAwBXBVA.html

推荐文章

  • linux的python如何进行进程管理

    在Linux中,可以使用Python的内置库os和subprocess进行进程管理 使用os.system()执行外部命令: import os # 执行外部命令,例如:ls
    os.system("ls") 使用...

  • linux的python怎样进行系统调用

    在Linux中,Python可以通过os和sys库进行系统调用 使用os库: os库提供了许多与操作系统交互的函数。例如,要使用os.system()执行系统命令,可以这样做:
    i...

  • python中set怎样进行集合排序

    在Python中,可以使用sorted()函数对集合(set)进行排序。sorted()函数返回一个排序后的列表(list),而不是集合,因为集合本身是无序的。以下是一个示例:

  • python中set如何进行集合转换

    在Python中,可以使用集合(set)的内置方法进行集合转换。以下是一些常用的集合转换方法: 将列表转换为集合: my_list = [1, 2, 3, 4, 5]
    my_set = set(m...

  • win10系统白图标问题怎么修复

    要修复Win10系统中的白图标问题,可以尝试以下方法: 清理图标缓存:按下Win + R键打开运行窗口,输入"cmd"并按下Enter键,然后输入以下命令并按下Enter键:

  • 笔记本电脑电池如何修复

    笔记本电脑电池修复的方法有以下几种: 校准电池:有时候电池的电量显示不准确,需要重新校准电池。首先将电脑使用到电池耗尽后自动关机,然后将电源适配器插入电...

  • vb如何把label设置为透明

    在VB中,可以通过设置Label控件的BackColor属性为Transparent来使Label透明。
    例如,以下代码将Label1设置为透明:
    Label1.BackColor = Color.Transpa...

  • vb怎么删除数据库指定的记录

    在VB中,你可以使用SQL语句来删除数据库中的指定记录。以下是一个示例代码,用于删除名为"表名"的表中特定ID的记录:
    Dim conn As New SqlConnection("连接...