基于python编写简单的名片管理系统
主程序和功能程序
主程序(main):提示用户输入信息,判断。启动入口
功能程序(tools):所有名片的功能函数
在写分支时不希望立即编写分支代码可以写pass关键字跳过【占位符】
如何debug
左键点击行号,-->debug-->F8(执行步骤分解)-->F9(debug结束)
TODO注释
想想为什么要这样做
字典:有键值姓名:李华,有对应关系
列表:
pycharm重命名
及时删除todo注释删除掉
cards_tools.py
点击查看代码
# 记录所有的名片字典
card_list = []
def show_menu():
"""显示菜单"""
print("*" * 50)
print("欢迎使用[名片管理系统]")
print("")
print("1.新建名片")
print("2.显示全部")
print("3.查询名片")
print("")
print("0.退出系统")
print("*" * 50)
def new_card():
"""新增名片"""
print("-" * 50)
print("新增名片")
# 提示用户输入名片的详细信息、
name = input("请输入姓名:")
phone = input("请输入电话:")
qq = input("请输入qq:")
email = input("请输入邮箱:")
# 使用用户输入的信息建立一个名片字典
card_dict = {"name": name, "phone": phone, "qq": qq, "email" :email}
# 将名片字典添加到列表中
card_list.append(card_dict)
print(card_list)
# 提示用户添加成功
print("%s名片添加成功" % name)
def show_all():
"""显示全部"""
print("-" * 50)
print("显示全部")
# 判断是否存在名片记录,没有的话提示用户,当前没有,请使用新增功能
if len(card_list) == 0:
print("没有名片记录,请使用新增功能")
return
# 打印表头
for name in ["姓名","电话","qq","邮箱"]:
print(name, end="\t\t\t")
print("")
# 打印分割线
print("=" * 50)
# 遍历名片列表依次输出字典信息
for card_dict in card_list:
print("%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t" % (card_dict["name"],card_dict["phone"],card_dict["qq"],card_dict["email"]))
def search_card():
"""搜索名片"""
print("-" * 50)
print("搜索名片")
# 1.提示用户要搜索的姓名
find_name = input("请输入要搜索的姓名:")
# 2.遍历名片列表,查询搜索的姓名,如果没有找到,需要提示用户
for card_dict in card_list:
if card_dict["name"] == find_name:
print("姓名\t\t\t电话\t\t\tqq\t\t\t邮箱\t\t\t")
print("=" * 50)
print("%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t" % (card_dict["name"],
card_dict["phone"],
card_dict["qq"],
card_dict["email"]))
# 针对找到的名片执行修改和删除操作
deal_card(card_dict)
break
else:
print("没有找到%s" % find_name)
def deal_card(find_dict):
action_str = input("请输入对名片的操作 "
"[1] 修改 [2] 删除 [0] 返回上级菜单")
if action_str == "1":
print("请输入修改信息")
find_dict["name"] = input_card_info(find_dict["name"],"姓名:")
find_dict["phone"] = input_card_info(find_dict["phone"], "电话:")
find_dict["qq"] = input_card_info(find_dict["qq"], "qq:")
find_dict["email"] = input_card_info(find_dict["email"], "邮箱:")
print("修改名片完成")
elif action_str== "2":
print("删除")
card_list.remove(find_dict)
def input_card_info(dict_value,tip_message):
"""
输入名片信息
:param dict_value:字典中原有的值
:param tip_message:输入文字提示内容
:return:如果输入有值就更新,没有值就返回原来的值
"""
# 1.提示用户输入内容
result_str = input(tip_message)
#2.针对用户的输入进行判断,如果输入了内容直接返回结果,
if len(result_str) > 0:
return result_str
# 3.如果用户没有输入内容,返回`字典中原来的值`
else:
return dict_value
cards_main.py
点击查看代码
import cards_tools
while True:
# 显示功能菜单
cards_tools.show_menu()
action_str = input("请选择希望执行的操作:")
print("您选择的操作是[%s]"% action_str)
# 1,2,3对名片进行操作
if action_str in ["1","2","3"]:
# 新增名片
if action_str == "1":
cards_tools.new_card()
# 显示全部
if action_str == "2":
cards_tools.show_all()
# 查询名片
if action_str == "3":
cards_tools.search_card()
# 0退出系统
elif action_str == "0":
print("欢迎再次使用名片系统")
break
# 别的输入,输入有误重新输入
else:
print("输入有误,请重新输入")
本文作者:罗论明
本文链接:https://www.cnblogs.com/lianshanspeak/p/16311019.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)