260行python结课作业|学生管理系统

Posted Eric%258436

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了260行python结课作业|学生管理系统相关的知识,希望对你有一定的参考价值。

本博客为个人整理Python学习帖,python 基础的结课作业,如有错误,感谢指正。系统学习,欢迎持续关注,后续会涉及自动化办公,爬虫,python大数据,算法,人工智能基础—对接AI。欢迎关注,python基础阶段结束,会更新AIOT相关博客,欢迎大家讨论相关AIOT技术,如有大佬愿意加入团队,欢迎私信。

"""
@author:想泡罗敷的马同学
@Email: mqa010225@163.com
@file:stuSystem.py
@time:2021/07/17/
"""
import os

filename = 'student.txt'


def main():
    while True:
        menu()
        choice = int(input('Please,choice!'))
        if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
            if choice == 0:
                answer = input('Are you sure?y/n')
                if answer == 'y' or answer == 'Y':
                    print('Thanks for your use,bye!')
                    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()
            else:
                continue


def menu():
    print('=======================stuSystem=======================')
    print('=========================menu==========================')
    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('Please input stuID(like 1001):')
        if not id:
            break
        name = input('Please input stuName:')
        if not name:
            break
        try:
            english = int(input('please input english score:'))
            python = int(input('please input python score:'))
            java = int(input('please input java score:'))
        except:
            print('Illegal input, please input integer, please re-enter')
            continue
        # 将录入的学生信息保存到字典中
        student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
        # 将学生信息添加到列表中
        student_list.append(student)
        answer = input('Is add again?y/n\\n')
        if answer == 'Y' or answer == 'y':
            continue
        else:
            break
    save(student_list)
    print('Student information input completed')


def save(lst):
    try:
        stu_txt = open(filename, 'a', encoding='utf-8')
    except:
        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:
        stu_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 != '':
                        if d['id'] == id:
                            student_query.append(d)
                    elif name != '':
                        if d['name'] == name:
                            student_query.append(d)
            shou_student(student_query)
            student_query.clear()
            answer = input('是否继续查询?y/n\\n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break
        else:
            print('暂未报错学生信息')


def shou_student(lst):
    if len(lst) == 0:
        print('没有查询到学生信息,无数据显示')
        return
    format_title = '{:^6}\\t{:^12}\\t{:^8}\\t{:^10}\\t{:^10}\\t{:^8}'
    print(format_title.format('ID', 'name', 'english', 'python', 'java', 'scoreSum'))
    format_data = '{:^6}\\t{:^12}\\t{:^8}\\t{:^10}\\t{:^10}\\t{:^8}'
    for item in lst:
        print(format_data.format(item.get('id'),
                                 item.get('name'),
                                 item.get('english'),
                                 item.get('python'),
                                 item.get('java'),
                                 int(item.get('english')) + int(item.get('python')) + int(item.get('java'))
                                 ))


def delete():
    while True:
        student_id = input('Please input student_ID:')
        if student_id != '':
            if os.path.exists(filename):
                with open(filename, 'r', encoding='utf-8') as file:
                    student_old = file.readlines()
                    print(student_old)
            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))
                        if d['id'] != student_id:
                            wfile.write(str(d) + '\\n')
                        else:
                            flag = True
                    if flag:
                        print('id为{student_id}的学生信息已被删除')
                    else:
                        print(f'没有找到id为{student_id}的学生信息')
            else:
                print('无学生信息')
                break
            show()
            answer = input('Is delete again?y/n')
            if 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')
    if student_id != '':
        with open(filename, 'w', encoding='utf-8') as wfile:
            for item in student_old:
                d = dict(eval(item))
                if d['id'] == student_id:
                    print('找到学生信息,可以修改他的相关信息了')
                    while True:
                        try:
                            d['name'] = input('请输入name:')
                            d['english'] = input('请输入english score:')
                            d['python'] = input('请输入python score:')
                            d['java'] = input('请输入java score:')
                        except:
                            print('你输入有错误,请重新输入')
                        else:
                            break
                    wfile.write(str(d) + '\\n')
                    print('修改成功!!!')
                else:
                    wfile.write(str(d) + '\\n')
            answer = input('是否继续修改其他学生信息?y/n\\n')
            if answer == 'y' or answer == 'Y':
                modify()
    else:
        return


def sort():
    show()
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            student_lst = rfile.readlines()
        student_new = []
        for item in student_lst:
            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()
    mode = input('请选择排序方式(1.english 2.python 3.java 0.总成绩)')
    if mode == '1':
        student_new.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool)
    elif mode == '2':
        student_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
    elif mode == '3':
        student_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
    elif mode == '0':
        student_new.sort(key=lambda x: int(x['english']) + int(x['java']) + int(x['python']), reverse=asc_or_desc_bool)
    else:
        print('你输入有错误,请重新输入')
        sort()
    shou_student(student_new)


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():
    student_lst = []
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            students = rfile.readlines()
            for item in students:
                student_lst.append(eval(item))
            if student_lst:
                shou_student(student_lst)
    else:
        print('暂无数据')


if __name__ == '__main__':
    main()

# 如何将python文件 打包 exe
# cmd pip install PyInstaller
# pyinstaller -F 路径+文件名.扩展名         打包文件
# 执行完 最后会有提示 .exe生成路径 生成到哪了 我的是在倒数第二行 appending archieve to 路径


以上是关于260行python结课作业|学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章

学生信息管理系统(C语言,带文件操作)

C++结课大作业——职工管理系统

JavaWeb登录注册系统/界面(邮箱验证码,数据库连接,详细注释,可作结课作业,可用于学习,可接入其他主系统)

python之路34面向对象作业之学生选课系统

HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段

Java实现飞机大战小游戏|Java入门结课作业