如何在获取和存储用户输入时不覆盖字典的值? (Python)

How to not overwrite a value of a dictionary when taking and storing a user's input? (Python)

我是一个 python 初学者,尝试制作一个联系人 book/address 图书程序。我获取用户 input/contact 信息(值)并将其作为值存储到字典中,但是当程序仍然 运行 时,如果我尝试输入新的联系信息(新值),它将覆盖现有的,我该如何解决这个问题?所以我可以在程序仍然 运行.

时添加任意数量的联系信息(值)

提前致谢。

这是我的代码:

def head():
    print("")
    print("========================")
    print("      Contact Book      ")
    print("========================")

def restart():
    response = input("\nOpen menu again? (yes/no): ").lower()
    if response == "yes":
        task()
    else:
        print("\nSee You next time!")

def task():
    head()
    done = False
    print('''1. Add Contact
2. Search
3. View Contact List
4. Delete All Contact
5. Exit''')
    while not done:
        task = input("\nWhat do You want to do? (1-5):")
        if task == "1":
            print("\nAdding a new contact!")
            global cnt_lst
            global new
            cnt_lst = {}  
            new = {}
            new['new_key'] = {}
            new['new_key']['Name '] = input("Name: ")
            new['new_key']['Phone'] = input("Phone: ")
            if not new['new_key']['Phone'].isnumeric():
                while not new['new_key']['Phone'].isnumeric():
                        print("Invalid input, please enter only a number!")
                        new['new_key']['Phone'] = input("Phone: ")
            new['new_key']['Email'] = input("Email: ")
            cnt_lst.update(new)
            print("\nContact is saved!")
            done = True
            restart()

        elif task == "2":
            search = input("\nSearch: ")
            info = False

            for key, value in cnt_lst.items():   
                for npe, val in value.items():
                    if search in val:
                        info = True
                        print("=" * 20)
                        for npe, val in value.items():
                            print(npe,' : ',val)
                        break

            if not info:
                print("\nNo info was found!")
            done = True
            restart()

        elif task == "3":
            if 'cnt_lst' not in globals():
                print("\nNo contact info available!")
            elif 'cnt_lst' in globals() and len(cnt_lst) == 0:
                print("\nNo contact info available!")
            else:
                print("\nAll Contact Info")
                for key, value in cnt_lst.items():   
                    for npe, val in value.items():
                        print("=" * 20)
                        for npe, val in value.items():
                            print(npe,' : ',val)
                        break
            done = True
            restart()

        elif task == "4":
            cnt_lst.clear()
            print("\nSuccesfully deleted all contact info!")
            done = True
            restart()

        elif task == "5":
            print("See You next time!")
            break

        else:
            print("Invalid input please enter a single number from 1 to 5")
            restart()
   
task()

我认为您的问题是每次您的用户按 1 输入新联系人时,您的代码如下所示:

print("\nAdding a new contact!")
            global cnt_lst
            global new
            cnt_lst = {}  # <- here you are erasing your dictionary
            new = {}

您应该将字典存储为文本文件或 XML 或 JSON,这样所有联系人都存储在本地,然后在程序开始时您可以阅读其内容以进行打印或添加新条目