首先新建项目,包含主程序cards_main和工具库cards_tools:
如下:
main函数主主要使用while和if实现4功能切换,切换的具体操作由cards_tools中的函数执行。不说了直接上代码,代码都比较简单:
(1)cards_main:
# _*_ coding : UTF-8 _*_
"""
File_name : cards_main.py
Author : jack
Date : 2018.7.18
Function : 名片管理系统主程序
"""
import cards_tools
# 使用while实现窗口的循环使用
while True:
cards_tools.cards_dis()
cards_input = input("请选择操作功能:")
if cards_input in ["1", "2", "3"]:
if cards_input == "1":
print("- " * 20)
print("您选择输入的操作代码是: 1" )
cards_tools.cards_add()
elif cards_input == "2":
print("- " * 20)
print("您选择输入的操作代码是: 2" )
cards_tools.cards_show_all()
elif cards_input == "3":
print("- " * 20)
print("您选择输入的操作代码是: 3" )
cards_tools.cards_requ()
elif cards_input == "0":
print("您选择输入的操作代码是: 0" )
print("即将退出系统......
" "已退出系统,欢迎您下次【名片管理系统】!")
break
else:
cards_input = input("输入错误,请重新输入:")
cards_tools.cards_dis()
#cards_input = input("输入错误,请重新输入:")
while True:
cards_input = input("输入错误,请重新输入:")
cards_tools.cards_dis()
if cards_input in ["1", "2", "3", "0"]:
if cards_input == "1":
print("- " * 20)
print("您选择输入的操作代码是: 1")
cards_tools.cards_add()
break
if cards_input == "2":
print("- " * 20)
print("您选择输入的操作代码是: 2")
cards_tools.cards_show_all()
break
if cards_input == "3":
print("- " * 20)
print("您选择输入的操作代码是: 3")
cards_tools.cards_requ()
break
else:
# #print("返回主菜单")
break
break
(2)cards_tools:
# _*_ coding : UTF-8 _*_
"""
File_name : cards_main.py
Author : jack
Date : 2018.7.18
Function : 名片管理系统tools
"""
# 显示菜单
def cards_dis():
"""显示菜单"""
print("* " * 20)
print(" 欢迎使用【名片管理系统 V1.0】
"
" 1. 新建名片
" " 2. 显示全部
" " 3. 查询名片
" " 0. 退出系统")
print("* " * 20)
cards_list = []
# 新增名片
def cards_add ():
"""新增名片"""
print("- " * 20 )
print("新增名片")
"""输入-整理成字典-将字典存储到列表中-提示输入成功"""
name_str = input("请输入姓名:")
gender_str = input("请输入性别:")
tel_int = input("请输入电话:")
add_str = input("请输入地址:")
cards_dict = {"name" : name_str,
"gender" : gender_str,
"telephone" : tel_int,
"address" : add_str}
cards_list.append(cards_dict)
print(cards_list)
print("%s 的名片新增成功!" % name_str)
# 显示全部
def cards_show_all ():
"""显示全部名片"""
print("- " * 20)
print("显示所有名片")
if len(cards_list) == 0:
print("当前系统中没有名片,请新增名片!")
else:
print("=" * 40)
#print("姓名 " "性别 " "电话 " "地址")
for name in ("姓名","性别","电话","地址"):
print(name, end=" ")
print(" ")
for cards_dict in cards_list:
print("%s %s %s %s" % (cards_dict["name"],
cards_dict["gender"],
cards_dict["telephone"],
cards_dict["address"]))
print(cards_dict)
print("=" * 40)
# 查询名片
def cards_requ ():
"""查询名片"""
print("- " * 20)
print("查询名片")
find_name = input("请输入要搜索的姓名:")
if len(cards_list) == 0:
print("抱歉!没找到用户 %s 的信息, 请新增!" % find_name)
else:
for search_name in cards_list:
if search_name["name"] == find_name:
print("找到 %s 的信息了。" % find_name)
print("=" * 40)
for name in ("姓名", "性别", "电话", "地址"):
print(name, end=" ")
print(" ")
print("s% s% s% s%" % (search_name["name"],
search_name["gender"],
search_name["telephone"],
search_name["address"]))
print("=" * 40)
cards_deal(search_name)
break
else:
print("抱歉!没找到用户 %s 的信息。" % find_name)
# 删除和修改名片信息
def cards_deal(find_dict):
"""修改和删除名片"""
deal_cards = input("请选择要进行的操作: [1] 删除 [2] 修改 [3] 返回主菜单
")
if deal_cards in ["1","2","3"]:
if deal_cards == "1":
cards_list.remove(find_dict)
print("删除用户 %s 名片成功!" % (find_dict["name"]))
if deal_cards == "2":
find_dict["name"] = cards_mod(find_dict["name"], "请输入修改后的姓名:")
find_dict["gender"] = cards_mod(find_dict["gender"], "请输入修改后的性别:")
find_dict["telephone"] = cards_mod(find_dict["telephone"], "请输入修改后的电话:")
find_dict["address"] = cards_mod(find_dict["address"], "请输入修改后的地址:")
print("用户 %s 的名片修改成功!" % find_dict["name"])
else:
print("输入错误,请重新输入!")
def cards_mod(cards_value, cards_new):
result = input(cards_new)
if len(result) > 0:
return result
else:
return cards_value
本人也是新手,欢迎大佬们批评指正。