基于字典的客户信息管理系统,使用列表嵌套字典完成。
代码逻辑参考【5.5Python之列表的嵌套使用】
在此基础上做优化处理:
# 初始化客户信息--列表嵌套字典
customers = [{"name": "Alice", "age": 25, "email": "alice@example.com"},{"name": "Bob", "age": 30, "email": "bob@example.com"},{"name": "Charlie", "age": 35, "email": "charlie@example.com"}
]while True:print("""--------------欢迎进入客户管理系统--------------1. 添加客户2. 删除客户3. 修改客户4. 查询一个客户5. 查询所有客户6. 退出
-------------------------------------------""")choice = input("请输入你的选择:")# 1. 添加客户if choice == "1":name = input("请填写姓名:")age = int(input("请填写年龄:"))email = input("请填写邮箱:")# 将原来的列表改为字典new_customer = {"name": name,"age": age,"email": email}customers.append(new_customer)print(f"你已添加新的客户信息:{new_customer}")# 2. 删除客户elif choice == "2":if len(customers) == 0:print(f"暂无客户信息!")else:delete_customer = input("请填写需要删除的客户姓名:")# 将原来的列表的索引改为字典的key值for customerD in customers:if customerD["name"] == delete_customer:customers.remove(customerD)print(f"你已删除客户的信息:{delete_customer}")breakelif (customers[len(customers) - 1])["name"] == customerD["name"]:print(f"查无此客户的信息:{delete_customer}")# 3. 修改客户elif choice == "3":if len(customers) == 0:print(f"暂无客户信息!")else:update_customer = input("请填写需要修改的客户姓名:")for customerD in customers:# 将原来的列表的索引改为字典的key值if customerD["name"] == update_customer:name = input("请填写新的姓名:")age = int(input("请填写新的年龄:"))email = input("请填写新的邮箱:")# 将原来的列表改为字典customerD.update({"name": name, "age": age, "email": email})print(f"你已将客户:{update_customer}的信息修改为:{customerD}")breakelif (customers[len(customers) - 1])["name"] == customerD["name"]:print(f"查无此客户的信息:{update_customer}")# 4. 查询一个客户elif choice == "4":if len(customers) == 0:print(f"暂无客户信息!")else:search_customer = input("请填写需要查询的客户姓名:")for customerD in customers:# 将原来的列表的索引改为字典的key值if customerD["name"] == search_customer:print(f"你查询的客户姓名: {customerD["name"]}, 年龄: {customerD["age"]}, 邮箱: {customerD["email"]}")breakelif (customers[len(customers) - 1])["name"] == customerD["name"]:print(f"查无此客户的信息:{search_customer}")# 5. 查询所有客户elif choice == "5":if len(customers) == 0:print(f"暂无客户信息!")else:print(f"客户信息如下:")for customerD in customers:# 将原来的列表的索引改为字典的key值print(f"姓名:{customerD["name"]},年龄:{customerD["age"]},邮箱:{customerD["email"]}")# 6. 退出elif choice == "6":print("你已退出系统,感谢你的使用!")break# 其他情况else:print("输入的参数有误,请重新输入!")
代码逻辑上只是把原来的列表里的列表的逻辑修改为字典的逻辑。