# 开发一个名片管理系统
# 包含功能:1.新建名片;2.显示全部;3.查询名片
# cards_main为主程序,负责程序的入口;cards_tool为辅助程序,实现不同的功能。
# 1. 导入模块
import cards_tool
import cards_input
import time
while_flag = 1; # 循环初始化
cards_input.cards_load()
while while_flag:
# 显示主功能界面(包含功能:1.新建名片;2.显示全部;3.查询名片;0.退出系统)
cards_tool.window_main()
user_input = int(input("请输入需要执行的功能:")) #判断用户输入
if user_input == 1:
# print("新建名片")
# cards_tool.print_center("新建用户名片",60)
cards_tool.new_card()
pass # 实现新建名片功能
elif user_input == 2:
# print("显示名片")
cards_input.cards_display()
flag_change = input("继续操作(1:修改名片 2:删除名片 0:退出系统 其他输入:返回主页)")
if flag_change =="1":
# 实现对应名片的修改操作
cards_input.card_change()
# print("-"*10+"对某名片进行修改操作"+"-"*10)
pass
# pass # 实现显示所有名片的功能
elif flag_change == "2":
cards_input.card_del()
pass
elif flag_change == "0":
while_flag = 0
else:
pass
elif user_input == 3:
# print("查询名片")
cards_input.card_check()
flag_change = input("继续操作(1:修改名片 2:删除名片 0:退出系统 其他输入:返回主页)")
if flag_change =="1":
# 实现对应名片的修改操作
cards_input.card_change()
# print("-"*10+"对某名片进行修改操作"+"-"*10)
pass
# pass # 实现显示所有名片的功能
elif flag_change == "2":
cards_input.card_del()
pass
elif flag_change == "0":
while_flag = 0
else:
pass
elif user_input == 0:
# print("系统退出!")
# cards_tool.user_exit()
break
else:
print("-"*10+"用户输入有误,请检查后重新输入!"+"-"*10)
continue
# user_re = input("是否继续运行名片系统(0:结束系统 其他输入:回到主页):") #判断用户输入
# if user_re == "0":
# break
# else:
# continue
# time.sleep(0.5) # 可以实现软件延迟
# 以下为退出循环后的操作
cards_input.file_save()
cards_tool.user_exit() # 最后一行,系统退出提醒
上面为主程序,下面为辅助模块。
# 主要实现对名片数据的存取,数据格式化操作
import cards_tool
# 1. 程序执行显示全部时,可以将之前存储的名片数据加载进来:cards.txt
cards = [] #程序执行前,cards列表初始化,用以保存所有的名片
def str_change(s,num):
"""
字符串合并,将列表s中每num个元素合并成一个元组,再将整体合并成一新的列表s_list
"""
str_flag = 0
s_list = []
for char in s:
if (str_flag % 4) == 0:
s_list.append(s[str_flag:str_flag+4:])
str_flag = str_flag+1
else:
str_flag = str_flag+1
pass
return s_list
def cards_load():
"""
加载之前存储过的所有名片信息
"""
# cards = [] #程序执行前,cards列表初始化,用以保存所有的名片
f_open = open("./Desktop/认识python/名片管理系统/cards.txt")
cards_raw = f_open.read()
f_open.close()
cards_list = cards_raw.split()
cards_list = cards_list[5::]
cards_list = str_change(cards_list,4)
# print(cards_list)
pass # 列表数据格式化
for card in cards_list:
load_card = {}
load_card["name"] = card[1]
load_card["age"] = card[2]
load_card["phone"] = card[3]
cards.append(load_card)
else:
str1 = "-"*10+"保存的名片数据加载完毕!"+"-"*10
print(str1.center(48))
# 2. 格式化用户名片数据
cards_title = ("序号","姓名","年龄","电话")
def check_exist(name):
flag = 0
for card in cards:
if card["name"] == name:
flag = flag+1
else:
pass
else:
return flag
def cards_display():
"""
格式化显示所有用户名片信息
"""
card_index = 1 #列表序号
cards_tool.print_center(cards_tool.return_star("*",60),60)
cards_tool.print_center("用户名片列表",60)
cards_tool.print_center(cards_tool.return_star("-",60),60)
for title in cards_title:
print(title, end = "\t")
else:
print()
for card in cards:
print(card_index, end = "\t")
card_index = card_index + 1
print(card["name"], end = "\t")
print(card["age"], end = "\t")
print(card["phone"], end = "\t")
print()
else:
cards_tool.print_center(cards_tool.return_star("*",60),60)
def card_check():
"""
用户名片查询
"""
flag_check = 0
cards_tool.print_center(cards_tool.return_star("*",60),60)
cards_tool.print_center("用户名片查询",60)
cards_tool.print_center(cards_tool.return_star("-",60),60)
check_name_c = input("请输入待查询用户姓名:") # 读取待查询用户姓名
cards_tool.print_center(cards_tool.return_star("-",60),60)
for card in cards:
if card["name"] == check_name_c:
for title in cards_title: # 输出表头
print(title, end = "\t")
else:
print()
print(1, end = "\t") # 输出待查询用户名片信息
print(card["name"], end = "\t")
print(card["age"], end = "\t")
print(card["phone"], end = "\t")
print()
flag_check = flag_check+1
else:
# print("-"*10+"查无此人,请确认姓名是否正确!"+"-"*10)
pass
else:
if flag_check == 0:
print("-"*10+"查无此人,请确认姓名是否正确!"+"-"*10)
else:
pass
cards_tool.print_center(cards_tool.return_star("*",60),60)
def card_change():
"""
对某一用户名片信息进行修改
"""
flag_change = 0
cards_tool.print_center(cards_tool.return_star("*",60),60)
cards_tool.print_center("用户名片修改",60)
cards_tool.print_center(cards_tool.return_star("-",60),60)
check_name_change = input("请输入待修改用户姓名:") # 读取待修改用户姓名
cards_tool.print_center(cards_tool.return_star("-",60),60)
for card in cards:
if card["name"] == check_name_change:
change_name = input("请输入新的用户姓名(n:该项不修改):")
change_age = input("请输入新的用户年龄(n:该项不修改):")
change_phone = input("请输入新的用户电话(n:该项不修改):")
if change_name == "n":
pass
else:
card["name"] = change_name
if change_age == "n":
pass
else:
card["age"] = change_age
if change_phone == "n":
pass
else:
card["phone"] = change_phone
print("-"*10+"用户 %s 的信息修改完成" % card["name"]+"-"*10)
flag_change = flag_change +1
else:
continue
else:
if flag_change == 0:
print("-"*10+"查无此人,请确认姓名是否正确!"+"-"*10)
cards_tool.print_center(cards_tool.return_star("*",60),60)
def card_del():
"""
对某用户的名片信息进行删除
"""
del_index = 0
flag_del = 0
cards_tool.print_center(cards_tool.return_star("*",60),60)
cards_tool.print_center("用户名片删除",60)
cards_tool.print_center(cards_tool.return_star("-",60),60)
check_name_del = input("请输入待删除用户姓名:") # 读取待删除用户姓名
cards_tool.print_center(cards_tool.return_star("-",60),60)
for card in cards:
if card["name"] == check_name_del:
del cards[del_index]
print("-"*10+"用户 %s 的名片信息删除完成!" % card["name"]+"-"*10)
# for title in cards_title: # 输出表头
# print(title, end = "\t")
# else:
# print()
# print(1, end = "\t") # 输出待查询用户名片信息
# print(card["name"], end = "\t")
# print(card["age"], end = "\t")
# print(card["phone"], end = "\t")
# print()
flag_del = flag_del+1
else:
# print("-"*10+"查无此人,请确认姓名是否正确!"+"-"*10)
pass
del_index = del_index+1 # 删除索引加1
else:
if flag_del == 0:
print("-"*10+"查无此人,请确认姓名是否正确!"+"-"*10)
cards_tool.print_center(cards_tool.return_star("*",60),60)
# 3. 程序结束时,将目前的名片保存进文件:cards.txt
def file_save():
save_index = 1
f_cards = open("./Desktop/认识python/名片管理系统/cards.txt","w")
f_cards.write("名片信息文本\n\n")
f_cards.close()
f_cards = open("./Desktop/认识python/名片管理系统/cards.txt","a")
for title in cards_title: # 输出表头
f_cards.write(title+"\t")
else:
f_cards.write("\n")
for card in cards: # 输出表头
f_cards.write(str(save_index)+"\t")
f_cards.write(card["name"]+"\t")
f_cards.write(card["age"]+"\t")
f_cards.write(card["phone"]+"\n")
save_index = save_index+1
else:
f_cards.close()
# 实现对名片系统的显示,新建,修改和查询功能
import cards_input
def return_star(char,num):
"""
打印num个char字符
"""
return char*num
def print_center(str,num):
"""
字符串对其居中
"""
print(str.center(num))
def window_main():
"""
系统主窗口显示
"""
print_center(return_star("*",60),60)
print_center("名片系统 V1.0",60)
print_center(return_star("-",60),60)
print("请输入需要执行的功能编号:".ljust(60))
print_center("1: 新建名片",60)
print_center("2: 显示全部",60)
print_center("3: 查询名片",60)
print_center("0: 退出系统",60)
print_center(return_star("-",60),60)
print("Presented by Hao".rjust(60))
print_center(return_star("*",60),60)
def user_exit():
"""
系统退出!
"""
print_center(return_star("*",60),60)
print_center("用户退出!",60)
print_center(return_star("*",60),60)
def new_card():
"""
新建用户
"""
print_center(return_star("*",60),60)
print_center("新建用户名片",60)
print_center(return_star("-",60),60)
new_card = {}
new_card["name"] = input("请输入新用户姓名:")
new_card["age"] = input("请输入新用户年龄:")
new_card["phone"] = input("请输入新用户电话:")
print_center(return_star("-",60),60)
print_center("新用户 %s 的名片创建成功!" % new_card["name"],60)
cards_input.cards.append(new_card) # 将新用户名片加入名片库中
# print(cards_input.cards)
print_center(return_star("*",60),60)
运行效果:
来源:CSDN
作者:bigSun2017
链接:https://blog.csdn.net/bigSun2017/article/details/104632421