"""
TODO 功能介绍
---------------------------------------------------------
菜单主页
1. 新建名片
2. 显示名片
3. 查询名片
0. 退出系统
---------------------------------------------------------
新建名片: 添加时有验证
性别仅分男/女
电话仅由11位数字组成
QQ仅由4-10位数字组成
邮箱注意@后缀是否正确
---------------------------------------------------------
显示名片:
---------------------------------------------------------
姓名 性别 电话 Q Q 邮箱
---------------------------------------------------------
1. 修改名片
修改内容可以为空,显示当前被修改名片个数,
可同时修改同一个人的多个名片
2. 删除名片
删除同一个人的多个名片
3. 返回菜单
---------------------------------------------------------
查询名片: 按姓名查询名片
---------------------------------------------------------
姓名 性别 电话 Q Q 邮箱
---------------------------------------------------------
退出系统: 感谢使用本系统,欢迎下次光顾!
---------------------------------------------------------
"""
import card_tools
# TODO 程序入口
def main():
while True:
card_tools.show_window() # TODO 显示系统页面
entry = input("请选择操作功能:")
if entry in ['1', '2', '3']:
if entry == '1': # TODO 新建名片
card_tools.add_card()
elif entry == '2': # TODO 显示名片
card_tools.show_all_card()
else: # TODO 查询名片
card_tools.select_card()
elif entry == '0': # TODO 退出系统
print("感谢使用本系统,欢迎下次光顾!")
break
else:
print("指令无法识别,请重新输入!")
print("正在重新加载,请稍后......")
# TODO 调用入口
main()
import tool_judge
# 初始化名片字典
card_dict = {"name": "",
"sex": "",
"M/P": "",
"QQ": "",
"Email": ""}
# 初始化名片列表
cards_list = []
# TODO 显示系统页面
def show_window():
print("-" * 57, "\n")
print("欢迎使用[名片管理系统]".center(26, " "), "\n")
print("1. 新建名片".center(26, " "))
print("2. 显示名片".center(26, " "))
print("3. 查询名片".center(26, " "))
print("0. 退出系统".center(26, " "), "\n")
print("-" * 57)
# TODO 输入个人信息
def push_news():
print("姓名:", end="")
card_dict["name"] = input()
print("性别:", end="")
card_dict["sex"] = tool_judge.sex_judge(input())
print("电话:", end="")
card_dict["M/P"] = tool_judge.mp_judge(input())
print("Q Q:", end="")
card_dict["QQ"] = tool_judge.qq_judge(input())
print("邮箱:", end="")
card_dict["Email"] = tool_judge.email_judge(input())
return card_dict
# TODO 输出个人信息
def pop_news():
print("-" * 57)
print("姓名".rjust(5, " "), end="\t")
print("性别".rjust(2, " "), end="\t")
print("电话".rjust(2, " "), end="\t")
print("Q Q".rjust(6, " "), end="\t")
print("邮箱".rjust(10, " "))
# TODO 显示名片
def show_all_card():
if len(cards_list) != 0:
pop_news() # 输出个人信息
for lists in cards_list:
print(lists["name"].rjust(5, " "), end="\t")
print(lists["sex"], end="\t")
print(lists["M/P"], end="\t")
print(lists["QQ"], end="\t")
print(lists["Email"])
print("-" * 57)
upd_del_show() # TODO 修改和删除页面入口
else:
print("暂无任何名片,请先添加新的名片!")
print("正在返回菜单,请稍后......")
# TODO 添加新的名片
def add_card():
while True:
print("-"*57)
print("新增名片")
cards_list.append(push_news().copy()) # 保存新增名片
print("-" * 57)
print("已保存")
print("1.继续添加".center(26, " "))
print("2.返回菜单".center(26, " "))
print("-" * 57)
entry = input("请选择操作功能:")
if entry == '1':
continue
elif entry == '2':
print("正在返回菜单,请稍后......")
break
else:
print("指令无法识别,请重新输入!")
# TODO 查询名片
def select_card():
if len(cards_list) != 0:
while True:
print("-" * 57)
print("按姓名查询名片:")
name = input("请输入姓名:")
count = 0 # 初始化被查询的名片个数
pop_news() # 输出个人信息
for lists in cards_list:
if lists["name"] == name:
count += 1
print(lists["name"].rjust(5, " "), end="\t")
print(lists["sex"], end="\t")
print(lists["M/P"], end="\t")
print(lists["QQ"], end="\t")
print(lists["Email"])
print("-" * 57)
if count == 0:
print('"%s"的名片不存在'.rjust(48, " ") % name)
print("-" * 57)
else:
print('有关用户"%s"的名片共有"%d"个'.rjust(48, " ") % (name, count))
print("-" * 57)
print("可进行以下操作:")
print("1.继续查找".center(24, " "))
print("其他任意键返回上一页".center(26, " "))
print("-" * 57)
entry = input("请选择操作功能:")
if entry == '1':
continue
else:
print("正在返回上一页,请稍后......")
break
else:
print("暂无任何名片,请先添加新的名片!")
print("正在返回菜单,请稍后......")
# TODO 修改个人名片
def modify_card():
print("-" * 57)
while True:
name = input("请输入要修改的名片姓名:")
count = 0 # 初始化要修改的名片个数
c = 0 # 初始化修改状态 0:未修改,1:已修改
for list1 in cards_list:
if list1["name"] == name:
count += 1
if count != 0:
print('有关用户"%s"的名片有%d个' % (name, count))
i = 1
for lists in cards_list:
if lists["name"] == name:
print('正在修改"%s"的第%d个名片...' % (name, i))
print("姓名:", end="")
name1 = input()
if len(name1) != 0:
lists["name"] = name1
print("性别:", end="")
sex = input()
if len(sex) != 0:
lists["sex"] = tool_judge.sex_judge(sex)
print("电话:", end="")
mp = input()
if len(mp) != 0:
lists["M/P"] = tool_judge.mp_judge(mp)
print("QQ:", end="")
qq = input()
if len(qq) != 0:
lists["QQ"] = tool_judge.qq_judge(qq)
print("邮箱:", end="")
email = input()
if len(email) != 0:
lists["Email"] = tool_judge.email_judge(email)
print("-" * 57)
if len(name1) == 0 and len(sex) == 0 and len(mp) == 0 and len(qq) == 0 and len(email) == 0:
print('"%s"的第%d个名片没有更改!' % (name, i))
print("-" * 57)
c = 1
if c == 0:
print('"%s"的第%d个名片已更改!' % (name, i))
print("-" * 57)
i += 1
else:
print('有关用户"%s"的名片不存在,请重新输入!' % name)
print("-" * 57)
print("可进行以下操作:")
print("1.继续修改".center(24, " "))
print("其他任意键返回上一页".center(28, " "))
print("-" * 57)
entry = input("请选择操作功能:")
if entry == '1':
continue
else:
print("正在返回上一页,请稍后......")
print("-" * 57)
break
# TODO 删除名片
def delete_card():
print("-" * 57)
while True:
count = 0 # 初始化要删除的名片个数
while True:
print("按姓名删除名片:")
name = input("请输入姓名:")
i = 0 # 初始化列表索引
if len(cards_list) == 0:
break
while True:
if i == len(cards_list):
break
lists = cards_list[i]
if lists["name"] == name:
cards_list.remove(lists)
count += 1
else:
i += 1
if len(cards_list) == 1 and lists["name"] != name:
i = 0
break
if len(cards_list) == 0:
print("删除成功!")
break
if count == 0:
print("输入的用户名片不存在,请重新输入!")
print("正在重新加载本页面,请稍后......")
break
else:
print("删除成功!")
count = 0
break
print("-" * 57)
if len(cards_list) == 0:
print("没有任何名片信息!")
break
print("可进行以下操作:")
print("1.继续删除".center(24, " "))
print("其他任意键返回上一页".center(26, " "))
print("-" * 57)
entry = input("请选择操作功能:")
print("-" * 57)
if entry == '1':
continue
else:
print("正在返回上一页,请稍后......")
break
python的删除列表操作(del,remove,pop)不支持一次性删除多个相同的数据,因此使用while比较方便.
大概步骤如下:
1.初始化索引i,判断列表的长度len(cards_list),大于0则继续,否则终止;
2.根据索引获取列表中的数据,与输入的姓名进行判断,如果相等则删除该列表中的数据,否则索引i加1指向下一个地址;
3.如果列表长度等于1,且当前获取的名字和输入的name不相等,则将索引重置为0,跳出循环;否则返回第2步.
# TODO 修改和删除页面入口
def upd_del_show():
while True:
print("可进行以下操作:")
print("1. 修改名片".center(26, " "))
print("2. 删除名片".center(26, " "))
print("3. 返回菜单".center(26, " "))
print("-" * 57)
entry = input("请选择操作功能:")
if entry == '1':
modify_card() # TODO 修改名片入口
elif entry == '2':
delete_card() # TODO 删除名片入口
elif entry == '3':
print("正在返回上一页,请稍后......")
print("-" * 57)
break
else:
print("指令无法识别,请重新输入!")
print("正在重新加载本页面,请稍后......")
print("-" * 57)
# TODO 判断性别是否输入正确
def sex_judge(sex):
if sex not in ["男", "女"]:
print("性别仅分男/女,请重新输入")
print("性别:", end="")
sex = sex_judge(input())
return sex
# TODO 判断电话是否输入正确
def mp_judge(mp):
if (not mp.isdecimal()) or (len(mp) != 11):
print("电话仅由11位数字组成,请重新输入")
print("电话:", end="")
mp = mp_judge(input())
return mp
# TODO 判断QQ是否输入正确
def qq_judge(qq):
if (not qq.isdecimal()) or (len(qq) not in range(4, 11)):
print("QQ仅由4-10位数字组成,请重新输入")
print("QQ:", end="")
qq = qq_judge(input())
return qq
# TODO 判断邮箱是否输入正确
def email_judge(email):
# 国内外常用邮箱后缀
email_suffix = ["@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com", "@aol.com",
"@ask.com", "@live.com", "@qq.com", "@0355.net", "@163.com",
"@163.net", "@263.net", "@3721.net", "@139.com", "@yeah",
"@ sohu.com", "@ msn.com", "@126.com", "@ sina.com",
"@ aim.com", "@ 21cn.com", "@ mail.com", "walla.com", "@ inbox.com",
"@tom.com", "@etang.com", "@eyou.com", "@56.com", "@x.cn", "@chinaren.com",
"@sogou.com", "@hongkong.com", "@citiz.com"
]
select_suffix = "@"
# 判断邮箱是否有@,并且@不是第一位
if email.find("@") not in [-1, 0]:
suffix = email[email.index(select_suffix):]
# 判断@后缀是否正确
if suffix not in email_suffix:
print("邮箱有误,请重新输入")
print("邮箱:", end="")
email = email_judge(input())
else:
print("邮箱有误,请重新输入")
print("邮箱:", end="")
email = email_judge(input())
return email
已上传下载链接: https://pan.baidu.com/s/1g9FgHK6dLC-WVvdZegApxg 提取码: 6fy2