加了文件,可使学生信息持久性保存在文件中。
注:即使退出循环或者程序,在下次进入系统的时候仍可读入学生信息数据。


一、代码

代码如下(示例):

def print_menu():
    print("""
    ==========================================
            欢迎适用学生信息管理系统
            1.添加学生信息
            2.删除学生信息
            3.修改学生信息
            4.查询学生信息
            5.统计学生成绩
            6.显示所有学生信息
            7.退出系统
    ==========================================
            """)

def print_info(stu_info):
    # print("学号",   "姓名",   "性别", "语文成绩", "数学成绩", "英语成绩",)
    print("学号   姓  名    性  别    语文成绩     数学成绩    英语成绩")
    for i in range(len(stu_info)):
        print(stu_info[i], end=" " * 7)
#1.添加学生信息
def apendStudent():
    print("你选择的选项是:  1.添加学生信息")
    id = input("请输入你要添加的学生学号:")
    for i in students:
        if len(i) > 0 and i[0] == id:
            print("该学生信息已保存")
            break
    else:
        stu_info = input_info(id)
        students.append(stu_info)
        answer = input('是否继续添加?y/n\n')
        if answer == 'y':
            apendStudent()
        else:
            save_file()
            print('学生信息录入完毕!!!')
        # save_file()
        # print("你添加的学生信息如下:")
        # print_info(stu_info)
#2.删除学生信息
def delStudent():
    print("你选择的选项是:2.删除学生信息")
    id = input("请输入你要删除的学生学号:")
    for i in students:
        if len(i) > 0 and i[0] == id:
            students.remove(i)
            print("信息已刪除")
            save_file()
            break
    else:
        print("学生信息未保存")
#3.修改学生信息
def modifyStudent():
    print("你选择的选项是:3.修改学生信息")
    id = input("请输入你要修改的学生学号:")
    for i in students:
        if len(i) > 0 and i[0] == id:
            stu_Id = students.index(i)
            stu_info = input_info(id)
            students[stu_Id] = stu_info
            print("修改后的学生信息:")
            print_info(stu_info)
            save_file()
            break
    else:
        print("学生信息未保存")
#4.查询学生信息
def searchStudent():
    print("你选择的选项是:4.查询学生信息")
    id = input("请输入你要查询的学生学号:")
    for i in students:
        if len(i) > 0 and i[0] == id:
            print("查询的学生信息如下:")
            print_info(i)
            break
    else:
        print("学生信息未保存")
#输入学生信息
def input_info(id):
    stu_id = id
    name = input("姓名:")
    sex = input("性别:")
    ch_score = float(input("语文成绩:"))
    math_score = float(input("数学成绩:"))
    eng_score = float(input("英语成绩:"))
    stu_info = [stu_id, name, sex, ch_score, math_score, eng_score]
    return stu_info
#5.统计学生成绩
def showStudent():
    print("你选择的选项是:5.统计学生成绩")
    id = input("请输入你要统计的学生学号:")
    for i in students:
        if len(i) > 0 and i[0] == id:
            stu_score(i)
            break
    else:
        print("学生信息未保存")
def stu_score(stu_info):
    sum_score = stu_info[3] + stu_info[4] + stu_info[5]
    avg_score = sum_score / 3
    print("学生三门课程总成绩为:%.2f" % sum_score)
    print("学生三门课程平均成绩为:%.2f" % avg_score)
#6.显示所有学生信息
def showAllStudent():
    print("你选择的选项是:6.显示所有学生信息")
    if len(students) > 0:
        print("学号  姓名  性别    语文成绩   数学成绩   英语成绩  总成绩  平均成绩")
        for i in range(len(students)):
            sum_scores = students[i][3] + students[i][4] + students[i][5]
            avg_scores = sum_scores / 3
            print(students[i][0],"  ",students[i][1],"  ", students[i][2],"  ",students[i][3],"  ",students[i][4],"  ",students[i][5],"  ",sum_scores,"  ",avg_scores,"\n")
    else:
        print("暂无数据!")
#保存数据至文件中
def save_file():
    f = open("student.txt", 'w', encoding='utf-8')
    f.write(str(students))
    f.close()
    print("数据保存至student.txt文件成功!")
#将数据读取至变量中
def read_file():
    try:
        f = open('student.txt', 'r', encoding='utf-8')
        ret = f.read()
        global students
        students = eval(ret)
        f.close()
        print("数据读取成功!")
    except:
        print("文件不存在")


stu_info = []
students = []

while True:
    print_menu();
    read_file();
    select_no = input("请输入你的操作选项编号:")
    if select_no == "1":
        apendStudent();
    elif select_no == "2":
        delStudent();
    elif select_no == "3":
        modifyStudent();
    elif select_no == "4":
        searchStudent();
    elif select_no == "5":
        showStudent();
    elif select_no == "6":
        showAllStudent();
    elif select_no == "7":
        print("退出系统!")
        break
    else:
        print("你输入的选项有误,请重新输入选项编号!")