This case uses a custom function and operation of the common string to determine knowledge statements and loops and so on.
Claim
- You must use a custom function, complete modularity of the program.
- Business card information includes at least: name, phone, address.
- You must complete functions: add, delete, change, and exit.
analysis
- First of all we, we should first define a global variable to store all the contact information.
cards = [{
"name": "张三",
"phone": "10086",
"address": "山西省",
}, {
"name": "李四",
"phone": "10010",
"address": "北京市",
}] # 定义一个的列表用于存放名片信息,默认里面有张三和李四的信息,方便以后调试用。
- Completion of addition, deletion, modification, function to find other related operations (Cards global variable is a variable type, it may also be no global increase in the function call), Note: as defined variables a: int = 0, int just pure Tip developer it is of type int, to facilitate debugging.
def print_menu():
""""完成打印功能菜单"""
print("=" * 20)
print(" 名片管理系统")
print(" 1:添加一个名片")
print(" 2:删除一个名片")
print(" 3:修改一个名片")
print(" 4:查询一个名片")
print(" 5:显示所有的名片")
print(" 6:退出")
print("=" * 20)
def add_card():
"""完成添加一个名片的功能"""
new_infor: dict = {
"name": input("请输入一个名字:"),
"phone": input("请输入一个电话:"),
"address": input("请输入一个地址:")
}
cards.append(new_infor)
print("添加成功!")
def delete_card():
del_name = input("请输入要删除的名字:")
for person in cards:
if del_name == person["name"]:
cards.remove(person)
print("删除成功!")
break
else:
print("找不到要删除的人!")
def update_card():
name: str = input("请输入要修改的名字(只能通过名字来修改电话和住址):")
for person in cards:
if name == person["name"]:
phone = input("请输入新的的电话(直接回车则不修改):")
address = input("请输入新的的地址(直接回车则不修改):")
if phone:
person["phone"] = phone
if address:
person["address"] = address
print("修改成功")
break
else:
print("找不到要修改的人!")
def find_card():
"""用来查询一个名片"""
find_name: str = input("请输入要查询的名字(支持模糊查询):")
flag: int = 1
for temp in cards:
# 遍历名片中的所有名字,判断要查找的名字是否存在,不存在则打印查无此人
if temp["name"].find(find_name) != -1:
print("%s\t%s\t%s" % (temp["name"], temp["phone"], temp["address"]))
flag = 0
if flag:
print("查无此人")
def show_all():
print("姓名\t电话\t住址")
for temp in cards:
print("%s\t%s\t%s" % (temp["name"], temp["phone"], temp["address"]))
- Finally, completion of the main function, and calls the main function
def main():
""""完成对整个程序的控制"""
# 打印功能提示
print_menu()
while True:
# 获取用户的选择
num: str = input("请输入功能序号:")
# 判断输入的是否为数字
if not num.isdigit():
print("请输入数字!")
continue
# 转换成数字类型
num: int = int(num)
# 增
if num == 1:
add_card()
# 删
elif num == 2:
delete_card()
# 改
elif num == 3:
update_card()
# 查
elif num == 4:
find_card()
elif num == 5:
show_all()
elif num == 6:
break
else:
print("请按号输入!")
print()
# 调用主函数
if __name__ == '__main__':
main()