1.显示欢迎界面,提示操作码,按照操作码执行程序
2.操作码功能包括:查询,查找,新增
3.查询出来后,可继续操作–>修改,删除
4.名片需要记录的信息–>编号,姓名,电话,邮箱,QQ。
5.编号自动生成
6.如果在功能选择界面连续输入三次错误的选项,则强行终止程序
分两部分开发完成,第一部分card_main,存储主要功能
第二部分card_tools 存储所有需要调用的函数:
"""需求总览:
1.显示欢迎界面,提示操作码,按照操作码执行程序
2.操作码功能包括:查询,查找,新增
3.查询出来后,可继续操作-->修改,删除
4.名片需要记录的信息-->编号,姓名,电话,邮箱,QQ,地址,身份证号等
5.编号自动生成
"""
import card_tools
code1 = 1
wrong = 0
while code1 == 1:
card_tools.show_man_menu()
code = input("请选择操作功能:")
if code == "0":
code1 = 0
print("欢饮再次使用名片管理系统")
elif code == "1":
card_tools.card_add()
card_tools.card_write_to_file()
elif code == "2":
card_tools.card_show_all()
card_tools.card_write_to_file()
elif code == "3":
card_tools.card_search()
card_tools.card_write_to_file()
card_tools.cards_number_auto_edit()
else:
wrong += 1
if wrong == 3:
print("您连续输错了三次,程序强制退出了")
break
else:
print("您的操作码选择错误,请重新输入(< _ >)")
continue
# 从文件test.txt读取之前存储的信息到cards列表
cards = []
with open("test.txt") as file_to_read:
file_1 = file_to_read.readlines()
for i in range(len(file_1)):
file_1[i] = file_1[i].replace("\n", "") # 去除文件中的换行符
file_2 = file_1[i].split() # 根据空格拆分成几个列表(将来的字典有几个键值对就有几个列表)
file_dic = {}
for j in range(len(file_2)):
file_3 = file_2[j].split(":") # 根据:把每个元素拆为两个列表
file_dic[file_3[0]] = file_3[1] # 把上面两个列表中的元素添加成一个字典的键值对
cards.append(file_dic)
def cards_number_auto_edit():
"""每次如果有执行删除等会修改编号的操作时,自动重排编号"""
count = 1
for auto_dic in cards:
auto_dic['number'] = count
count += 1
def card_write_to_file():
"""每次又修改或者其他操作后,把修改完成的列表重新写入到文件中"""
with open("test.txt", "w") as file_to_edit:
for info_dic in cards:
for info_str in info_dic:
file_to_edit.writelines(info_str)
file_to_edit.writelines(":")
file_to_edit.writelines(str(info_dic[info_str]))
file_to_edit.writelines(" ")
file_to_edit.writelines("\r")
def show_man_menu():
"""显示主菜单"""
print("*"*40, "\n", "欢迎使用【名片管理系统】v1.0",)
print("\r")
print("功能菜单列表")
print("1. 新建名片", "2. 显示全部", "3. 查询名片", sep='\n')
print("\r")
print("0. 退出系统")
print("*"*40)
def show_head_table():
"""输出表头"""
print("编号", "姓名", "电话", "邮箱", "QQ号", sep=' '*20)
print("-"*100)
def card_add():
"""新增名片"""
num = int(len(cards))
card_new = {}
card_new["number"] = num + 1
name_new = input("请输入姓名:")
card_new["name"] = name_new
phone_new = input("请输入电话:")
card_new["phone"] = phone_new
mail_new = input("请输入邮箱:")
card_new["mail"] = mail_new
qq_new = input("请输入QQ号:")
card_new["QQ"] = qq_new
cards.append(card_new)
print("添加成功")
print("以下是你添加的详细信息")
card_write_to_file()
show_head_table()
if card_new["number"] < 10:
print(card_new["number"], end=' '*23)
elif card_new["number"] < 100:
print(card_new["number"], end=' '*22)
else:
print(card_new["number"], end=' ' * 21)
print(card_new["name"], end=' '*(24 - len(card_new["name"])))
print(card_new["phone"], end=' '*(24 - len(card_new["phone"])))
print(card_new["mail"], end=' '*(24 - len(card_new["mail"])))
print(card_new["QQ"], end=' '*(24 - len(card_new["QQ"])))
print('\n')
def card_show_all():
"""显示所有名片"""
show_head_table()
for card_dic in cards:
if int(card_dic["number"]) < 10:
print(card_dic["number"], end=' ' * 23)
else:
print(card_dic["number"], end=' ' * 22)
print(card_dic["name"], end=' ' * (24 - len(card_dic["name"])))
print(card_dic["phone"], end=' ' * (24 - len(card_dic["phone"])))
print(card_dic["mail"], end=' ' * (24 - len(card_dic["mail"])))
print(card_dic["QQ"], end=' ' * (24 - len(card_dic["QQ"])))
print('\r')
print("\r")
def card_search():
"""查询名片,修改名片,删除名片"""
code2 = 0
while code2 == 0:
search_name = input("请输入你要查询的姓名:")
for cards_dic in cards:
if cards_dic["name"] == search_name:
list_index = cards.index(cards_dic)
print("以下是您查询出来的信息:")
show_head_table()
if int(cards_dic["number"]) < 10:
print(cards_dic["number"], end=' ' * 23)
else:
print(cards_dic["number"], end=' ' * 22)
print(cards_dic["name"], end=' ' * (24 - len(cards_dic["name"])))
print(cards_dic["phone"], end=' ' * (24 - len(cards_dic["phone"])))
print(cards_dic["mail"], end=' ' * (24 - len(cards_dic["mail"])))
print(cards_dic["QQ"], end=' ' * (24 - len(cards_dic["QQ"])))
print('\r')
print("-"*100)
# 对名片的操作
operate = input("请选择您要对改名片进行的操作:1-->修改|2-->删除|3-->只是看看,不做操作:")
if operate == "3":
code2 += 1
break
# 修改
elif operate == "1":
edit_name = input("请输入新的姓名(直接回车不做修改):")
edit_phone = input("请输入新的电话(直接回车不做修改):")
edit_mail = input("请输入新的邮箱(直接回车不做修改):")
edit_qq = input("请输入新的QQ号码(直接回车不做修改):")
if edit_name == '':
pass
else:
cards_dic["name"] = edit_name
if edit_phone == '':
pass
else:
cards_dic["phone"] = edit_phone
if edit_mail == '':
pass
else:
cards_dic["mail"] = edit_mail
if edit_qq == '':
pass
else:
cards_dic["QQ"] = edit_qq
print("修改完成!!!")
print("以下是您修改完成的信息:")
card_write_to_file()
show_head_table()
if int(cards_dic["number"]) < 10:
print(cards_dic["number"], end=' ' * 23)
else:
print(cards_dic["number"], end=' ' * 22)
print(cards_dic["name"], end=' ' * (24 - len(cards_dic["name"])))
print(cards_dic["phone"], end=' ' * (24 - len(cards_dic["phone"])))
print(cards_dic["mail"], end=' ' * (24 - len(cards_dic["mail"])))
print(cards_dic["QQ"], end=' ' * (24 - len(cards_dic["QQ"])))
print('\r')
print("-" * 100)
code2 += 1
break
# 删除
elif operate == "2":
your_choice1 = input("请确认是否删除该条记录!(回车确认,输入任何字符表示不删除:)")
if your_choice1 == '':
print("删除成功!!!")
card_write_to_file()
cards.pop(list_index)
code2 += 1
else:
code2 += 1
break
else:
your_choice = input("没找到你要查询的信息,请确定是否要继续查找:y-->继续查找、n-->不了")
if your_choice == "n":
code2 += 1
部分执行结果显示: