Python学习笔记(八)- 名片管理系统-增删改查等基础操作
代码目标:
实现一个基础的名片管理系统。
实现以下功能:
1、输入判断;
2、增添新的名片;
3、删除名片;
4、修改已有名片;
5、查询给定信息的名片。
cards_main.py 的代码:
1 import tools 2 3 # pass # pass占位符,如果不需要立刻编写的话,可以使用pass保证程序代码结构正确。 4 5 while True: 6 tools.menu() # 显示功能菜单 7 str = input("Please choose operation:") 8 print("Your choice is [%s]" % str) 9 10 # 输出123是名片的操作,0退出系统 11 if str in ["1", "2", "3"]: 12 if str == "1": # todo add 13 tools.new_card() 14 elif str == "2": # todo display all 15 tools.show_all() 16 else: # todo query 17 tools.serch_card() 18 elif str == "0": 19 print("Goodbye!") 20 else: 21 print("Your choice is wrong,please again!")
tools.py的代码(增删改查的基础操作):
1 card_list = [] # 所有名字字典 2 3 4 def menu(): 5 """ 6 显示菜单 7 """ 8 print("*" * 50) 9 print("Welcome!") 10 print("") # 输出空行 11 print("1. 新增名片") 12 print("2. 显示全部") 13 print("3. 搜索名片") 14 print("") 15 print("0. 退出系统") 16 print("*" * 50) 17 18 19 def new_card(): 20 print("-" * 50) 21 print("新增名片") 22 name = input("Please enter your name") 23 phone = input("Please enter your phone") 24 card_dict = {"name": name, 25 "phone": phone} 26 card_list.append(card_dict) 27 print("添加 %s 的姓名成功" % name) 28 29 30 def show_all(): 31 print("-" * 50) 32 print("显示所有名片") 33 34 # 判断是否存在名片记录 35 if len(card_list == 0): 36 print("当前没有任何名片记录,请添加") 37 return 38 39 # 打印表头 40 for name in ["NAME", "PHONE"]: 41 print(name, end="\t") 42 print("") 43 print("=" * 50) 44 for i in card_list: 45 print("%s\t\t%s\t" % (i["name"], i["phone"])) 46 47 48 def serch_card(): 49 print("-" * 50) 50 print("搜索名片") 51 # 提示用户需要搜索的姓名 52 find_name = input("请输入要搜索的姓名: ") 53 for i in card_list: 54 if i["name"] == find_name: 55 print("姓名\t\t电话") 56 print("=" * 50) 57 print("%s\t\t%s\t" % (i["name"], i["phone"])) 58 59 # 处理已经找到的名片(修改、删除) 60 del_card(i) 61 break 62 else: # 不是通过break退出的将会执行此句代码 63 print("抱歉,没有找到 %s !" % find_name) 64 65 66 def del_card(find_dict): 67 print(find_dict) 68 str = input("Please choose your choice: " 69 "1.modify 2.delete 3.return ") 70 if str == "1": 71 find_dict["name"] = input_change(find_dict["name"], "name: ") 72 find_dict["phone"] = input_change(find_dict["phone"], "phone: ") 73 print("modify successed !") 74 elif str == "2": 75 print("delete successed !") 76 card_list.remove(find_dict) 77 else: 78 return 79 80 81 def input_change(value, tips): 82 str = input(tips) 83 if len(str) > 0: 84 return str # 修改的值 85 else: 86 return value # 原本的值
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」