Comprehensive application - business card management system
Dark horse programmer python basic project notes
Target
- variable
- process control
- function
- module
system requirement
-
- Start the program, display the welcome interface of the business card management system, and display the function menu
-
- The user selects different functions with numbers
-
- According to the function selection, perform different functions
-
- The user business card needs to record the user's name , phone number , QQ , email
-
- If the specified business card is found, the user can choose to modify or delete the business card
step
- frame building
- Add business card
- show all business cards
- Query business card
- After the query is successful, modify and delete the business card
- Allow Python programs to run directly
01. Framework construction
Target
- Build the frame structure of business card management system
- Prepare the file , determine the file name, and ensure that the code can be written in the required position
- Write the main run loop to implement basic user input and judgment
1.1 Document preparation
- New
cards_main.py
Save the main program function code- program entry
- Every time the business card management system is started,
main
it is started through this file
- Create a function
cards_tools.py
to save all business cards- Encapsulate functions such as adding , querying , modifying , and deleting business cards in different functions
1.2 Write the main run loop
cards_main
Add an infinite loop to
while True:
# TODO(小明) 显示系统菜单
action = input("请选择操作功能:")
print("您选择的操作是:%s" % action)
# 根据用户输入决定后续的操作
if action in ["1", "2", "3"]:
pass
elif action == "0":
print("欢迎再次使用【名片管理系统】")
break
else:
print("输入错误,请重新输入")
String judgment
if action in ["1", "2", "3"]:
if action == "1" or action == "2" or action == "3":
- Use to judge
in
against the listor
, avoid using to concatenate complex logical conditions - User input is not used to
int
convert, which can avoid the program running error once the user input is not a number
pass
pass
It is an empty statement that does nothing and is generally used as a placeholder statement- is to maintain the integrity of the program structure
Infinite loop
- When developing software, if you do not want the program to exit immediately after execution
- An infinite loop can be added to the program
- It is up to the user to decide when to exit the program
TODO comments
- Followed
#
byTODO
, used to mark the work that needs to be done
# TODO(作者/邮件) 显示系统菜单
1.3 cards_tools
Add four new functions in
def show_menu():
"""显示菜单
"""
pass
def new_card():
"""新建名片
"""
print("-" * 50)
print("功能:新建名片")
def show_all():
"""显示全部
"""
print("-" * 50)
print("功能:显示全部")
def search_card():
"""搜索名片
"""
print("-" * 50)
print("功能:搜索名片")
1.4 Importing modules
cards_main.py
Use theimport
importcards_tools
module in
import cards_tools
- Modify
while
the code for the loop as follows:
import cards_tools
while True:
cards_tools.show_menu()
action = input("请选择操作功能:")
print("您选择的操作是:%s" % action)
# 根据用户输入决定后续的操作
if action in ["1", "2", "3"]:
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("输入错误,请重新输入:")
So far:
cards_main
all the codes in all have been developed!
1.5 Completion show_menu
function
def show_menu():
"""显示菜单
"""
print("*" * 50)
print("欢迎使用【菜单管理系统】V1.0")
print("")
print("1. 新建名片")
print("2. 显示全部")
print("3. 查询名片")
print("")
print("0. 退出系统")
print("*" * 50)
02. Store the structure of business card data
Programs are used to process data, and variables are used to store data
- Use a dictionary to record the details of each business card
- Use the list to uniformly record all business card dictionaries
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-0do78vbR-1689908530510)(./images/015/001_Business card management system global list.png)]
Define business card list variable
cards_tools
Add a list variable at the top of the file
# 所有名片记录的列表
card_list = []
Notice
- All business card-related operations need to use this list, so it should be defined at the top of the program
- When the program just runs, there is no data , so it is an empty list
03. Add business card
3.1 Function Analysis
- Prompt the user to enter business card information in turn
- Save business card information to a dictionary
- Add dictionary to card list
- Prompt that the business card is added
3.2 Implement the new_card method
- Follow the steps to implement the code
def new_card():
"""新建名片
"""
print("-" * 50)
print("功能:新建名片")
# 1. 提示用户输入名片信息
name = input("请输入姓名:")
phone = input("请输入电话:")
qq = input("请输入 QQ 号码:")
email = input("请输入邮箱:")
# 2. 将用户信息保存到一个字典
card_dict = {
"name": name,
"phone": phone,
"qq": qq,
"email": email}
# 3. 将用户字典添加到名片列表
card_list.append(card_dict)
print(card_list)
# 4. 提示添加成功信息
print("成功添加 %s 的名片" % card_dict["name"])
Tip: In
PyCharm
, you can use toSHIFT + F6
modify the variable name uniformly
04. Show all business cards
4.1 Function Analysis
- Loop through the business card list and display the information of each dictionary in sequence
4.2 Basic code implementation
def show_all():
"""显示全部
"""
print("-" * 50)
print("功能:显示全部")
for card_dict in card_list:
print(card_dict)
- The display effect is not good!
4.3 Add title and use \t
display
def show_all():
"""显示全部
"""
print("-" * 50)
print("功能:显示全部")
# 打印表头
for name in ["姓名", "电话", "QQ", "邮箱"]:
print(name, end="\t\t")
print("")
# 打印分隔线
print("=" * 50)
for card_dict in card_list:
print("%s\t\t%s\t\t%s\t\t%s" % (card_dict["name"],
card_dict["phone"],
card_dict["qq"],
card_dict["email"]))
4.4 Increase the judgment that there is no business card record
def show_all():
"""显示全部
"""
print("-" * 50)
print("功能:显示全部")
# 1. 判断是否有名片记录
if len(card_list) == 0:
print("提示:没有任何名片记录")
return
Notice
- Use in a function
return
to indicate return - If
return
there is no content after , it just means that the function will not execute subsequent codes until this point
05. Query business card
5.1 Function Analysis
- Prompt the user for a name to search for
- Iterate through the list based on the name entered by the user
- After searching for the specified business card, perform subsequent operations
5.2 Code implementation
- Realization of query function
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邮箱")
print("-" * 40)
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"]))
print("-" * 40)
# TODO(小明) 针对找到的字典进行后续操作:修改/删除
break
else:
print("没有找到 %s" % find_name)
- Add business card operation function: modify / delete / return to main menu
def deal_card(find_dict):
"""操作搜索到的名片字典
:param find_dict:找到的名片字典
"""
print(find_dict)
action_str = input("请选择要执行的操作 "
"[1] 修改 [2] 删除 [0] 返回上级菜单")
if action == "1":
print("修改")
elif action == "2":
print("删除")
06. Modification and deletion
6.1 Delete business card after successful query
- Since the dictionary records found are already saved in the list
- To delete a business card record, just delete the corresponding dictionary in the list
elif action == "2":
card_list.remove(find_dict)
print("删除成功")
6.2 Modify name card
- Since the dictionary records found are already saved in the list
- To modify the business card record, you only need to modify the data of each key-value pair in the corresponding dictionary in the list
if action == "1":
find_dict["name"] = input("请输入姓名:")
find_dict["phone"] = input("请输入电话:")
find_dict["qq"] = input("请输入QQ:")
find_dict["email"] = input("请输入邮件:")
print("%s 的名片修改成功" % find_dict["name"])
Modify business card refinement
- If the user does not want to modify some business card content when using it , what should be done? —— Since the function provided by the system
input
cannot meet the requirements, then define a new function to expand the functioninput_card_info
of the systeminput
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
Shebang
07. The symbol ( #!
) on LINUX
#!
This symbol is calledShebang
orSha-bang
Shebang
Usually used at the beginning of the first lineUnix
of a system script- Specify the interpreter to execute this script file
Steps to use Shebang
-
- Use
which
to querypython3
the path where the interpreter is located
- Use
$ which python3
-
- Modify the main python file to be run , add the following to the first line
#! /usr/bin/python3
-
- Modify the file permissions of the main python file and increase the execution permissions
$ chmod +x cards_main.py
-
- Execute the program when needed
./cards_main.py
Complete code reproduction
- card_main.py
# #! python解释器
import card_tools
while True:
# 显示功能菜单
card_tools.show_menu()
action_str = input("请选择希望执行的操作: ")
print("您选择的操作是【%s】" % action_str)
if action_str in ["1", "2", "3"]:
if action_str == "1":
card_tools.new_card()
if action_str == "2":
card_tools.show_all()
if action_str == "3":
card_tools.search_card()
pass
elif action_str == "0":
print("欢迎再次使用【名片管理系统】")
break
# pass
else:
print("您输入的不正确,请重新选择")
- card_tools.py
card_list = []
def show_menu():
"""显示菜单"""
print("*"*50)
print("欢迎使用【名片管理系统V1.0】")
print(" ")
print("1.新增名片")
print("2.显示全部")
print("3.搜索名片")
print(" ")
print("0.退出系统")
print("*" * 50)
def new_card():
"""新增名片"""
print("-" * 50)
print("新增名片")
name_str = input("请输入姓名:")
phone_str = input("请输入电话:")
qq_str = input("请输入QQ:")
email_str = input("请输入邮箱:")
card_dict = {
"name": name_str,
"phone": phone_str,
"qq": qq_str,
"email": email_str
}
card_list.append(card_dict)
print("添加 %s 的名片成功!" % name_str)
def show_all():
"""显示所有名片"""
print("-" * 50)
print("显示所有名片")
if len(card_list) == 0:
print("当前没有任何名片记录,请使用新增功能添加名片!")
return
for name in ["姓名", "电话", "QQ", "邮箱"]:
print(name, end = "\t\t")
print(" ")
print("="*50)
for card_dict in card_list:
print("%s\t\t%s\t\t%s\t\t%s" % (card_dict["name"], card_dict["phone"], card_dict["qq"], card_dict["email"]))
def search_card():
"""搜索名片"""
print("-" * 50)
print("搜索名片")
find_name = input("请输入要查找的姓名:")
for card_dict in card_list:
if card_dict["name"] == find_name:
print("找到了")
for name in ["姓名", "电话", "QQ", "邮箱"]:
print(name, end = "\t\t")
print(" ")
print("=" * 50)
print("%s\t\t%s\t\t%s\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):
# print(find_dict)
action_str = input("请选择要执行的操作【1】 修改 【2】 删除 【0】 返回上一级目录")
if action_str == "1":
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":
card_list.remove(find_dict)
print("删除名片成功")
def input_card_info(dict_value, tip_message):
result_str = input(tip_message)
if len(result_str) > 0:
return result_str
else:
return dict_value