26.名片管理系统

  1. 名片管理系统框架搭建
    1. 字符串判断
    2. pass
    3. 无限循环
    4. TODO注释
    5. cards_main.py
    6. cards_tools.py

名片管理系统框架搭建

1,准备文件,确定文件名,保证能够在需要的位置编写代码
2,编写主运行循环,实现基本的用户输入和判断

字符串判断

1,使用 in 针对列表判断,避免使用or 拼接复杂的逻辑条件
2,没有使用int 转换用户输入,可以避免一旦用户输入的不是数字,导致程序运行出错

pass

pass就是一个空语句,不做任何事情,一般用作站位语句,是为了保持程序结构的完整性

无限循环

在开发软件时,如果不希望程序执行后立即退出,可以在程序中增加一个无限循环,由用户来决定退出程序的时机

TODO注释

在 # 后跟上 TODO ,用于标记需要去做的工作
# TODO(xxx) xxx
return 可以返回一个函数的执行结果,下方的代码不会被执行,如果return后面没有任何内容,表示会返回到调用函数的位置并且不返回任何结果

注释文档在写完函数之后,别忘了写

cards_main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cards_tools
while True:
cards_tools.show_menu()

action = input("请选择操作功能")
print("您的输入是【%s】" % action)
"""
前面是没有加int的话
可以用 if act in ["1", "2", "3"]
然后用 elif,else
"""
# 新增名片
if action == "1":
cards_tools.new_card()
# 显示全部
elif action == "2":
cards_tools.show_all()
# 查询名片
elif action == "3":
cards_tools.search_card()
# 退出系统
elif action == "0":
print("欢迎再次使用系统")
break
else:
print("您的输入有误,请重新输入")

cards_tools.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

# 记录所有的名片的字典
card_list = []


def show_menu():
"""
显示菜单
:return:
"""
print("*"*20)
print("欢迎使用名片管理系统\n1.新建名片\n2.显示全部\n3.查询名片\n\n0.退出系统")
print("*"*20)


def new_card():
"""
新增名片
:return:
"""
print("-"*20)
print("新增名片")
# 1.提示用户输入名片的详细信息
name_str = input("请输入姓名")
phone_str = input("请输入电话")
qq_str = input("请输入qq")
email_str = input("请输入邮箱")
# 2.使用用户输入的信息建立一个名片字典
card_dict = {"name": name_str,
"phone": phone_str,
"qq": qq_str,
"email": email_str}
# 3.讲名片字典添加到列表中
card_list.append(card_dict)
print(card_list)
# 4.提示用户添加成功
print("%s 添加成功" % name_str)


def show_all():
"""显示所有名片"""
# 判断是否存在名片记录,如果没有,提示并返回
if len(card_list) == 0:
print("没有名片记录")
# return 可以返回一个函数的执行结果,下方的代码不会被执行,如果return后面没有任何内容,表示会返回到调用函数的位置并且不返回任何结果
return
# 打印表头
print("-"*20)
print("显示所有名片")
for name in ["name", "phone", "qq", "email"]:
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("-"*20)
print("查找名片")
# 1.提示用户输入要搜索的名字
find_name = input("请输入要搜索的姓名:")

# 2.遍历名片列表,查询要搜索的名字,如果没有找到,需要提示用户
for card_dict in card_list:
if find_name == card_dict["name"]:
print("找到了")
print("name\t\t\tphone\t\t\tqq\t\t\temail")
print("="*20)
print("%s\t\t\t%s\t\t\t%s\t\t\t%s" % (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):
"""
处理查找到的名片
:param find_dict: 查找到的名片
"""
print(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"], "phone:")
find_dict["qq"] = input_card_info(find_dict["qq"], "qq:")
find_dict["email"] = input_card_info(find_dict["email"], "email:")
elif action_str == "2":
card_list.remove(find_dict)
print("删除名片成功")


def input_card_info(dict_value, tip_message):
"""
修改
1.提示用户输入
2,针对用户输入进行判断,如果用户输入了内容,直接返回结果
3,如果没有输入,返回原来的值
:param dict_value: 字典中原有值
:param tip_message: 输入提示文字
:return: 若输入有信息,则更新信息,若没输入,则返回原来信息
"""
result_str = input(tip_message)
if len(result_str) > 0:
return result_str
else:
return dict_value

转载请注明来源,欢迎指出任何有错误或不够清晰的表达。可以邮件至gxnucgb@qq.com

文章标题:26.名片管理系统

文章字数:1.1k

本文作者:陈桂彬

发布时间:2019-08-03, 11:18:48

最后更新:2019-08-03, 16:54:25

原始链接:https://github.com/gxnucgb/gxnucgb.github.io/2019/08/03/26-名片管理系统/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏

github