学生信息管理系统(python实现)

学生信息管理系统(python实现),第1张

学生信息管理系统(python实现) 好嘛,经过三天的复习python和三天的敲代码,用python写了个学生信息管理系统,功能不是很完善,大家图一乐。
import os # 磁盘文件的库

filename = 'student.txt' # 学生信息保存的文件
# 主菜单
def menu():
    print("-------------------------------学生信息管理系统--------------------------------")
    print("tttt1.录入学生信息")
    print("tttt2.查找学生信息")
    print("tttt3.删除学生信息")
    print("tttt4.修改学生信息")
    print("tttt5.排序")
    print("tttt6.统计学生总人数")
    print("tttt7.显示所有学生总信息")
    print("tttt0.退出")

# 保存信息到txt文件中
def save(lst):
    # open()函数的用法,创建一个叫filename的文件,用追加方式打开,采用UTF-8编码
    #https://www.runoob.com/python/python-func-open.html
    stu_txt = open(filename,'a',encoding='utf-8') 
    for items in lst:
        stu_txt.write(str(items) + 'n')
    stu_txt.close() # 关闭文件

#录入学生信息
def insert():
    while True:
        id = input("请输入ID(如1001) ")
        if not id:
            break
        name = input("请输入姓名 ")
        if not name:
            break
        English = input("请输入英语成绩 ")
        if not English:
            break
        student = {'id':id,'name':name,'English score':English} # 创建学生的字典
        student_list = []
        student_list.append(student) # 将学生字放到列表中
        answer = input("是否继续添加学生信息 ")
        if answer == "y" or answer == "Y":
            continue
        else:
            break
    save(student_list) # 调用save()函数存储学生信息

# 显示的格式
def showstudent(lst):
    if len(lst) == 0:
        print("无此人信息")
        return
    # 定义标题显示的格式
    else:
        for items in lst:
            print(items)
    return
        
# 删除学生信息
def delete():
    while True:
        student_id = input('请输入想要删除学生的id ')
        if student_id: # 如果学生的ID存在的话
            if os.path.exists(filename):
                with open(filename,'r',encoding='utf-8') as file:
                    student_old = file.readlines() #file.readlines()读取文件所有行并返回列表
            else:
                student_old = [] #创建一个空列表
            flag = False
            if student_old:
                with open(filename,'w',encoding='utf-8') as wfile:
                    for items in student_old:
                        #print(items)
                        # 字符串转字典要用eval()函数(传送门在下面)
                        # https://www.runoob.com/python/python-func-eval.html
                        d = eval(items) 
                        if d['id'] != student_id:
                            wfile.write(str(d) + 'n')
                        else:
                            flag = True
                    if flag:
                        print('id为' + student_id +'的学生信息已被删除')
                    else:
                        print('没有找到id为' + student_id + '的学生信息')
        else:
            print("没有找到学生信息")
            break
        answer = input("是否继续删除学生信息 ")
        if answer == "y" or answer == "Y":
            continue
        else:
            print("删除完成")
            return    

# 查找学生信息
def search():
    id = ''
    name = ''
    while True:
        student_query = []
        if os.path.exists(filename):
            mode = input("按照id查询请输入1,按照姓名查询请输入2 ")
            if mode == '1':
                id = input("请输入想要查询的学生的id ")
            elif mode == '2':
                name = input("请输入学生的姓名 ")
            else:
                print("您的输入有误,请重新输入 ")
                search()
            with open(filename,'r',encoding='utf-8') as rfile:
                student = rfile.readlines()
                for items in student:
                    d = eval(items)
                    if id != '':
                        if d['id'] == id:
                            student_query.append(d)
                    if name != '':
                        if d['name'] == name:
                            student_query.append(d)
                showstudent(student_query)
            answer = input("是否继续查询学生信息 ")
            if answer == "y" or answer == "Y":
                search()
            else:                     
                return
        else:
            print("查无此人")
            return
  
#修改学生信息
def modify():
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            student_old = rfile.readlines()
    else:
        return
    student_id = input('请输入想要修改的学员的id ')
    with open(filename,'w',encoding='utf-8') as wfile:
         for items in student_old:
                d = dict(eval(items)) # 将字符串转为字典
                #print(d)
                if d['id'] == student_id:
                    print("找到学生信息了,可以进行修改")
                    while True:
                        d['name'] = input("请输入姓名 ")
                        d['English score'] = input("请输入英语成绩 ")
                        wfile.write(str(d) + 'n')
                        print("修改成功!!")
                        showstudent(list(d.keys()))
                        showstudent(list(d.values()))
                        break
                else:
                    wfile.write(str(d) + 'n')
                answer = input("是否继续修改学生信息 ")
                if answer == "y" or answer == "Y":
                     modify()
                else:                     
                     return
            
# 统计总人数
def total():
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            students = rfile.readlines()
            number = len(students)
            if students:
                print("共有" + str(number) + "名学生")
            else:
                print("还没有录入学生信息")
    else:
        print("暂未保存数据信息——————————")

# 显示总人数
def show():
    student_list = []
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            students = rfile.readlines()
            for items in students:
                student_list.append(eval(items))
            if student_list:
                showstudent(student_list)
    else:
        print("暂未保存数据")
    
# 排序
def sort():
    show()
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            student_list = rfile.readlines()
            student_new = []
            for item in student_list:
                d = dict(eval(item))
                student_new.append(d)
    else:
        return
    asc_or_desc = input("请选择排序方式 0为升序 1位降序")
    if asc_or_desc == '0':
        asc_or_desc_bool = False
    elif asc_or_desc == '1':
        asc_or_desc_bool = True
    else:
        print("您输入错误,请您重新输入")
        sort()
    
    student_new.sort(key=lambda student_new : student_new['English score'],reverse=asc_or_desc_bool)
 
    showstudent(student_new)

# 主函数       
def main():
    while True:
        menu();
        choice = int(input("请选择 "))
        if choice in range(0,8):
            if choice == 0:
                answer = input("您确定要退出系统吗?(y/n)")
                if answer == 'y' or answer == 'Y':
                    print('感谢您的使用')
                    break # 退出系统
                else:
                    continue
            elif choice == 1:
                insert()
            elif choice == 2:
                search()
            elif choice == 3:
                delete()
            elif choice == 4:
                modify()
            elif choice == 5:
                sort()
            elif choice == 6:
                total()
            elif choice == 7:
                show()   
            
main()
        





欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5689228.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存