“”"
Created on Thu Dec 30 16:38:44 2021
功能:文件型学生管理系统
作者:李虹菲
@author: asus
“”"
import os
card_infors=[]
def print_menu():
‘’’
打印菜单
:return: 无
‘’’
print()
print("" * 30)
print(“学生姓名管理系统V2.0”)
print(“1、添加学生姓名”)
print(“2、显示所有姓名”)
print(“3、查找学生姓名”)
print(“4、修改学生姓名”)
print(“5、删除学生姓名”)
print(“6、退出”)
print("" * 30)
def add_infor():
‘’’
添加学生名片
:return: 无
‘’’
stu = {}
new_name = input(“请输入姓名:”)
new_sex = input(“请输入性别:”)
new_classes = input(“请输入班级:”)
new_phone = input(“请输入电话号码:”)
# 将上述信息添加到字典中
stu[“name”] = new_name
stu[“sex”] = new_sex
stu[“classes”] = new_classes
stu[“phone”] = new_phone
# print(stu)
global card_infors
card_infors.append(stu)
print(“添加成功!”)
def show_infor():
‘’’
显示学生名片
:return:无
‘’’
if len(card_infors) == 0:
print(“目前还没有名片信息!”)
else:
print(“序号\t姓名\t\t\t性别\t班级\t手机号”)
for i in range(len(card_infors)):
print("{}\t{}\t\t\t{}\t{}\t{}".format(i + 1, card_infors[i]["name"], card_infors[i].get("sex"),
card_infors[i].get("classes"),card_infors[i]["phone"]))
def find_infor():
‘’’
查找学生名片
:return:无
‘’’
find_name = input(“请输入要查找的姓名:”)
for i in range(len(card_infors)):
if card_infors[i].get(“name”) == find_name:
print(“找到了,它是第{}张名片”.format(i + 1))
print(“名片信息如下:”)
print("{}\t{}\t{}\t{}".format(card_infors[i].get(“name”), card_infors[i][‘sex’],
card_infors[i][“classes”], card_infors[i][‘phone’]))
break
else:
print(“姓名为{}的名片不存在”.format(find_name))
def modify_infor():
‘’’
修改学生名片
:return:无
‘’’
find_name = input(“请输入要修改的姓名:”)
global card_infors
for i in range(len(card_infors)):
if card_infors[i].get(“name”) == find_name:
while True:
print(“1、修改姓名 2、修改性别 3、修改班级 4、修改手机号 5、修改结束”)
answer = input(“请输入要修改的功能序号:”)
if answer == “1”:
new_name = input(“请输入修改后的姓名:”)
card_infors[i][“name”] = new_name
elif answer == “2”:
new_sex = input(“请输入修改后的性别:”)
card_infors[i][“sex”] = new_sex
elif answer == “3”:
new_class = input(“请输入修改后的班级:”)
card_infors[i][“classes”] = new_class
elif answer == “4”:
new_phone = input(“请输入修改后的手机号:”)
card_infors[i][“phone”] = new_phone
elif answer == “5”:
# print(“修改结束”)
break
else:
print(“输入错误!”)
print(“修改成功!”)
break
else:
print(“修改失败,姓名为{}的名片不存在。”.format(find_name))
def delete_infor():
‘’’
删除名片
:return:无
‘’’
find_name = input(“请输入要删除的姓名:”)
global card_infors
for dic in card_infors:
if dic[“name”] == find_name:
card_infors.remove(dic)
print(“删除成功”)
break
else:
print(“删除失败,该名片不存在!”)
def save_infor():
with open(“back.txt”,“w”)as f:
f.write(str(card_infors))
def load_infor():
global card_infors
if os.path.exists(‘back.txt’):
with open(“back.txt”)as f:
content=f.read()
card_infors=eval(content)
def main():
load_infor()
# os.path.file()
while True:
# 1、打印菜单
print_menu() # 函数调用
choice = input("请输入功能序号:")
if choice == '1':
add_infor()
elif choice == '2':
show_infor()
elif choice == '3':
# 查找学生名片(根据姓名查找)
find_infor()
elif choice == '4':
# 修改学生名片(根据姓名找到要修改的学生名片)
# 查找学生名片(根据姓名查找)
modify_infor()
elif choice == '5':
# 删除名片(根据姓名定位到名片,然后再删除)
delete_infor()
elif choice == '6':
save_infor()#保存数据
print("谢谢使用,程序结束!")
break
else:
print("输入错误!\n")
main()