• python学生管理系统


    import os
    import re

    #获取本机用户名,构建student.txt文件名创建在左面
    import getpass
    username=getpass.getuser()
    print("当前登录用户名"+username)
    filename="student.txt"
    filename="C:\\Users\\"+username+"\\Desktop"+"\\"+filename
    print(filename)
    def main():
    ctrl=True
    while ctrl:
    menu()
    option=input("请选择>>>:")
    option_str=re.sub("\D","",option)
    if option_str in ["0","1","2","3","4","5","6","7"]:
    option_int=int(option_str)
    if option_int==0:
    print("你已经退出学生管理系统")
    ctrl=0
    elif option_int==1:
    insert()
    elif option_int==2:
    search()
    elif option_int==3:
    delete()
    elif option_int==4:
    modify()
    elif option_int==5:
    sort()
    elif option_int==6:
    total()
    elif option_int==7:
    show()

    def menu(): #first page
    print(
    """
    学生信息管理系统

    ===============功能菜单===============
    |
    | 1 录入学生信息
    | 2 查找学生信息
    | 3 删除学生信息
    | 4 修改学生信息
    | 5 排序
    | 6 统计学生总人数
    | 7 显示所有学生信息
    | 0 退出管理系统
    =======================================
    说明:通过数字或↑↓方向键选择菜单
    """
    )
    def insert(): #录入学生信息
    studentlist=[]
    mark=True
    while mark:
    id=input("请输入id如(1001):")
    if not id:
    break
    name=input("请输入姓名")
    if not name:
    break
    try:
    english =int(input("请输入英语成绩"))
    python = int(input("请输入python成绩"))
    c = int(input("请输入c语言成绩"))
    except:
    print("输入成绩格式无效")
    continue
    student={"id":id,"name":name,"english":english,"python":python,"c":c}
    studentlist.append(student)
    inputmark=input("是否继续添加(Y/N):")
    if inputmark=="Y" or inputmark=="y":
    mark=1
    else:
    mark=0
    print(studentlist)
    save(studentlist)
    print("学生信息录入完毕")




    def search():
    mark=True
    student_query=[]
    while mark:
    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") as file:
    student=file.readlines()
    for list in student:
    d=dict(eval(list))
    if id is not "":
    if d["id"]==id:
    student_query.append(d)
    elif name is not "":
    if d["name"]==name:
    student_query.append(d)
    show_student(student_query)
    student_query.clear()
    inputmark=input("是否继续查询(y/n):")
    if inputmark =="y" or inputmark=="Y":
    mark=True
    else:
    mark=False
    else:
    print("暂未保存数据信息")
    return
    def delete():
    mark=True
    while mark:
    studentid=input("请输入你要删除的学生id,如果输入为空则退出删除")
    if studentid=="":
    return 1
    if studentid is not "": #此处判断输入是否为空,如果输入有效执行读取信息
    if os.path.exists(filename):
    with open(filename,"r") as rfile:
    student_old=rfile.readlines()
    else: #如过为获取到文件内容,则返回空列表。stuent_ol=[]=false
    student_old=[]
    ifdel=False
    if student_old:
    with open(filename,"w") as wfile:
    d={}
    for list in student_old:
    d=dict(eval(list))
    if d['id']!=studentid:
    wfile.write(str(d)+"\n")
    #此处相当于读取了所有学生信息,除了id为输入要删除的id以外,其他学生的信息全部重新写入
    else: #如果 d['id']!=studentid ,那么ifdel=true,没有写入证明删除成功
    ifdel=True
    if ifdel:
    print("id号为%s的学生信息已经删除"%studentid)
    else:
    print("没有找到id为%s的学生信息"%studentid)
    else:
    print("无学生信息")
    break
    show()
    inputmark=input("是否继续删除(Y/N):")
    if inputmark=="y" or inputmark=='Y':
    mark=True
    else:
    mark=False
    def modify():
    show()
    if os.path.exists(filename):
    with open(filename,"r") as rfile:
    student_old=rfile.readlines()

    else:
    return
    studentid=input("请输入你要修改的学生信息的id:")
    with open(filename,"w") as wfile:
    for student in student_old:
    d=dict(eval(student))
    if d['id']==studentid:
    print("找到该学生id,可以修改信息")
    while 1:
    try:
    d["name"]=input("请输入姓名")
    d["english"] =int( input("请输入英语成绩"))
    d["python"] = int(input("请输入python成绩"))
    d["c"] = int(input("请输入C语言成绩"))
    except:
    print("输入信息有误,请重新输入")
    else:
    break
    student=str(d)
    wfile.write(student+"\n") #修改后的信息写入文件
    print("信息修改成功")
    else:
    wfile.write(student) #未修改的内容重新写入
    mark = input("是否继续修改(Y/N):")
    if mark == "y" or mark == "Y":
    modify()
    else:
    return 1

    def sort():
    show() #首先显示全部学生信息
    if os.path.exists(filename):
    with open(filename,"r") as file:
    student_old=file.readlines()
    student_new=[]
    for list in student_old:
    d=dict(eval(list))
    student_new.append(d) #一个列表中的元素为字典,每个元素存储每个学生信息的字典
    else:
    return
    ascordesc=input("请选择(0升序 / 1降序):")
    if ascordesc=="0":
    ascbool=False
    if ascordesc=="1":
    ascbool=True
    else:
    print("你的输入有误,请重新输入")
    sort()
    mode=input("""请选择排序方式(1 按英语成绩排序 2 按python成绩排序 3 按c语言成绩排序 0 按总成绩排序 )""")
    if mode=="1":
    student_new.sort(key=lambda x:x["english"],reverse=ascbool)
    elif mode=="2":
    student_new.sort(key=lambda x:x["python"],reverse=ascbool)
    elif mode=="3":
    student_new.sort(key=lambda x:x["c"],reverse=ascbool)
    elif mode=="0":
    student_new.sort(key=lambda x: x["english"]+x["python"]+x["c"], reverse=ascbool)
    else:
    print("输入有误,请重新输入")
    sort()
    show_student(student_new)


    def total():
    if os.path.exists(filename):
    with open(filename,"r") as rfile:
    student_old=rfile.readlines()
    if student_old:
    print("一共有%d名学生"%len(student_old))
    else:
    print("还没有录入学生信息")
    else:
    print("暂时未保存数据信息")
    def show():
    student_new=[]
    if os.path.exists(filename):
    with open(filename,"r") as rfile:
    student_old=rfile.readlines()
    for list in student_old:
    student_new.append(eval(list))
    if student_new:
    show_student(student_new)
    else:
    print("暂未找到数据信息")
    def save(student):
    try:
    students_txt=open(filename,"a")
    except Exception as e:
    students_txt=open(filename,"w")
    for info in student:
    students_txt.write(str(info)+"\n")
    students_txt.close()
    def show_student(studentList):
    if not studentList:
    print("!!--无数据信息--!!\n")
    return
    format_title="{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}"
    print(format_title.format("id","姓名","英语成绩","python成绩","语言成绩","总成绩"))
    format_data="{:^6}{:^12}\t{:^12}\t{:^12}\t{:^12}\t{:^12}"
    for info in studentList:
    print(format_data.format(info.get("id"),info.get("name"),str(info.get("english")),
    str(info.get("python")),str(info.get("c")),
    str(info.get("english")+info.get("c")+info.get("python")).center(12)
    ))
    main()
  • 相关阅读:
    Flutter 中的基本路由
    BottomNavigationBar 自定义 底部导航条
    StatefulWidget 有状态组件
    flutte页面布局四
    flutter页面布局三
    flutter页面布局二
    设计模式-工厂方法模式
    设计模式-代理模式
    设计模式-装饰模式
    SpringBoot项目部署到服务器上,tomcat不启动该项目
  • 原文地址:https://www.cnblogs.com/menxin/p/11580447.html
Copyright © 2020-2023  润新知