python--学生管理系统--统计学生总人数

# cyy python study
#开发时间:2022/6/10 14:18
import os
filename='student.txt'
def main():#一直调用主菜单 让用户选择
    while True:
        menu()
        choice=int(input('请选择:'))
        if choice in [0,1,2,3,4,5,6,7]:
            if choice==0:#退出系统
                answer=input('您确定要退出系统吗y/n:')
                if answer == 'y' or answer== 'Y':
                    print('感谢您的使用!!!')
                    break
            if choice==1: #录入学生信息
                insert()
            if choice==2:#查找
                search()
            if choice==3:#删除
                delete()
            if choice==4:#修改
                modify()
            if choice==5:#排序
                sort()
            if choice==6:#统计
                total()
            if choice==7:#显示所有信息
                show()
def menu():
    print('============================学生信息管理系统=================================')
    print('--------------------------------功能菜单------------------------------------')
    print('\t\t\t\t\t\t1.录入学生信息')
    print('\t\t\t\t\t\t2.查找学生信息')
    print('\t\t\t\t\t\t3.删除学生信息')
    print('\t\t\t\t\t\t4.修改学生信息')
    print('\t\t\t\t\t\t5.排序')
    print('\t\t\t\t\t\t6.统计学生总人数')
    print('\t\t\t\t\t\t7.显示所有学生信息')
    print('\t\t\t\t\t\t0.退出系统')
    print('--------------------------------------------------------------------------')

def insert():#录入学生信息
    student_list=[] #用于添加学生信息
    #循环录入学生信息
    while True:
        id = input('请输入ID(如1001):')
        if not id: #每个对象都有id ,空的为false
            break
        name = input('请输入姓名:')
        if not name:
            break
        #不是空的继续录入学生信息--成绩有可能录错---执行try
        try:
            english=int(input('请输入英语成绩:'))
            math=int(input('请输入数学成绩:'))
            chinese=int(input('请输入语文成绩:'))

         # 异常处理机制
        except:
            print('输入无效,不是整数类型请重新输入')
            continue #结束本次循环 进入下一次循环
        #录入没问题 将录入的学生信息保存到字典中---键值对
        student={'id':id,'name':name,'english':english,'math':math,'chinese':chinese}
        #将录入的学生信息添加到列表中
        student_list.append(student)
        answer=input('是否继续添加 y/n:')
        if answer == 'y' or answer == 'Y':
            continue
        else:
            break
    #创建之后还要保存在文件中
    #调用save函数 存储学生信息(跳出循环中)
    save(student_list)
    print('学生信息录入完毕!!!')

def save(lst):
    # 将列表中的内容添加到文件中,如果有追加,如果没有创建
    #可能出错 需要异常处理
    try:
        stu_txt=open(filename,'a',encoding='utf-8')#追加模式打开

    except:
        print('chu')
        stu_txt=open(filename,'w',encoding='utf-8') #写入的模

    for item in lst:#遍历列表
        stu_txt.write(str(item)+'\n')

    stu_txt.close()

def search():#查找学生信息
    student_query=[] #定义列表
    while True:
        id =''
        name=''
        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 item in student:
                    d=dict(eval(item))#列表转字典
                    if id !='':#按照ID查找
                        if d['id']==id:
                            student_query.append(d)#将字典添加到列表中
                    elif name!='':#按照姓名查找
                        if d['name']==name:
                            student_query.append(d)
            #显示查询结果
            show_student(student_query)
            #清空列表
            student_query.clear()
            #是否继续
            answer=input('是否继续查询 ? y/n :')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break
        else:
            print('暂未保存学生信息')
            return #跳出函数

def show_student(lst):#显示查询结果
    if len(lst)==0:#列表数据为0
        print('没有查询到学生信息,无数据显示!!!')
        return #直接退出函数
    #定义标题显示格式 --格式化字符串
    format_title=' {:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8} '
    print(format_title.format('ID','姓名','英语成绩','数学成绩','语文成绩','总成绩'))
    #定义内容的显示格式
    format_data=' {:^6}\t{:^12}\t{:^12}\t{:^6}\t\t{:^12}\t{:^8} '
    for item in lst:
        print(format_data.format(item.get('id'),#字典
                                 item.get('name'),
                                 item['english'],
                                 item['math'],
                                 item['chinese'],
                                 int(item['english'])+int(item['math'])+int( item['chinese'])
                                 ))


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()#读取所有数据放入列表中
            else:
                student_old =[]#文件不存在是空列表
            flag=False #标记是否删除

            if student_old:#判断列表 不是空列表 学生信息数据
                with open(filename,'w',encoding='utf-8') as wfile:#只写模式 将删除内容将原有内容覆盖
                    d ={}#空字典

                    #删除之后的加入写入文件中
                    for item in student_old:#遍历列表
                        d=dict(eval(item)) #列表中读出的字符串转成字典类
                        #判断删除的学生在字典中存在不存在
                        #遍历第一个键值对 如果字典中要删除id键值对的存在->flag->之后键值对不存在->由于跳过了一个转换 所以相当于删除
                        if d['id']!=student_id:#要删除的内容字典中不存在的话 #使用键获取数据
                            wfile.write(str(d)+'\n')#将字典数据转化成字符串写入
                        else:
                            flag=True
                    if flag:
                        print(f'ID为{student_id}的学生信息已被删除')
                    else:
                        print(f'没有找到ID为{student_id}的学生信息')
            else:
                print('无学生信息')#列表中没有找到
                break
            show()#删除之后要重新显示所有学生信息
            answer=input('是否继续删除?y/n:')
            if answer =='y' or answer=='Y':
                continue
            else:
                break


def modify():#修改学生信息
    show()#显示所有学生信息
    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 item in student_old:#遍历学生信息的列表
            #文件读取的内容为空时,eval()会报错处理,所以应当在使用该函数之前增加一个非空判断
            d = dict(eval(item))  #转换成字典
            if d['id'] == student_id:
                print('找到学生信息,可以修改他的相关信息!!!')
                while True:
                    try:
                        d['name']=input('请输入姓名:')
                        d['english'] = int(input('请输入英语成绩:'))
                        d['math'] = int(input('请输入数学成绩:'))
                        d['chinese'] = int(input('请输入语文成绩:'))
                    except:
                        print('您的输入有错误,请重新输入')
                    else:
                        break #该机制 没有异常执行else 有异常 执行except
                wfile.write(str(d)+'\n')
                print('修改成功!!!')
            else:
                wfile.write(str(d)+'\n')

        answer=input('是否继续修改其他学生信息?y/n: ')
        if answer == 'y' or answer == 'Y':
            modify() #自己写自己 实现了重复的功能

def sort():#排序
    pass
def total():#统计学生总人数
    if os.path.exists(filename):#判断文件是否存在
        with open(filename,'r',encoding='utf-8') as rfile:
            students=rfile.readlines()
            if students: #不是空列表
                print(f'一共有{len(students)}名学生') #实际上是判断列表的长度
            else:#文件存在 但是没数据
                print('还没有录入学生信息')

    else:
        print('暂未保存数据!!!')

def show():#显示所有学生信息
    pass

 #只有语句运行main的时候 这个输出语句才会运、不加的则其他中调用此模块函数 也会输出
if __name__ =='__main__':
    main()

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值