Python3

系统功能结构

程序设计

项目打包及演示



[Python3] 项目练习-学生管理系统

  • 1 系统功能结构
  • 2 程序设计
  • 3 项目打包及演示


1 系统功能结构

项目管理软件python源码 python管理系统项目_后端

  • 七大功能模块
  1. 录入学生信息模块
  2. 查找学生信息模块
  3. 删除学生信息模块
  4. 修改学生信息模块
  5. 学生成绩排名模块
  6. 统计学生总人数模块
  7. 显示全部学生信息模块

2 程序设计

import os
import traceback

menu_dict = {'1': '录入学生信息', '2': '查找学生信息', '3': '删除学生信息', '4': '修改学生信息',
            '5': '排序', '6': '统计学生总人数', '7': '显示学生所有信息', '0': '退出系统'}
filename = 'student.txt'

class StuMagSys(object):

    @classmethod
    def menu(cls):
        print('='*20 + '学生信息管理系统' + '='*20)
        print('-' * 24 + '功能菜单' + '-' * 24)
        for it in menu_dict:
            print(f'\t\t    {it}. {menu_dict[it]}')

    @staticmethod
    def save(lst):  #追加保存
        with open(filename, 'a+', encoding='utf-8') as stu_txt:
            for it in lst:
                stu_txt.write(str(it) + '\n')

    @staticmethod
    def isFileExist():
        if os.path.exists(filename):
            return True
        else:
            print('学生信息文件不存在!')
            return False

    @staticmethod
    def show_info(*args):
        if not args:
            with open(filename, 'r', encoding='utf-8') as rf:
                lst = rf.readlines()
        else:
            lst = args[0]  # *args返回值为元组
        if not lst:
            print('还未录入学生信息!')
        else:
            format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'  # 定义标题显示格式
            print(format_title.format('ID', '姓名', '英语成绩', 'Python成绩', 'Java成绩', '总成绩'))
            format_data = '{:^6}\t{:^12}\t{:^10}\t{:^10}\t{:^10}\t{:^10}'  # 定义内容的显示格式
            for it in lst:
                if not args:
                    it = eval(it)   #列表中存放的是每名学生信息的字典,在文件中是以字符串形式存储的,用eval函数去除引号
                print(format_data.format(it.get('id'),
                                         it.get('name'),
                                         it.get('english'),
                                         it.get('python'),
                                         it.get('java'),
                                         int(it.get('english')) + int(it.get('python')) + int(it.get('java'))))

    def insert(self):
        stu_list = []
        while True:
            stu_id = input('请输入id(如1001): ')
            if not stu_id:
                break
            name = input('请输入姓名: ')
            if not name:
                break
            try:
                english = int(input('请输入英语成绩: '))
                python = int(input('请输入python绩: '))
                java = int(input('请输入java绩: '))
            except:
                print('[insert]输入有误,请重新输入')
                continue
            student = {'id': stu_id, 'name': name, 'english': english, 'python': python, 'java': java}  #学生信息存到字典
            stu_list.append(student)
            answer = input('是否继续添加? y/n\n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break

        self.save(stu_list)

    def delete(self):
        while True:
            stu_id = input('请输入删除学生的id: ')
            if not stu_id:
                continue
            else:
                with open(filename, 'r', encoding='utf-8') as rf:
                    stu_lst = rf.readlines()

                delete_flag = False
                if stu_lst:
                    with open(filename, 'w', encoding='utf-8') as wf:   #以只写方式打开,每次都会重写写入
                        for it in stu_lst:
                            d = eval(it)
                            if d['id'] != stu_id:
                                wf.write(it)
                            else:
                                delete_flag = True  #删除 即不重新将此数据写入文件
                        if delete_flag:
                            print(f'id为{stu_id}的学生信息已被删除!')
                        else:
                            print(f'没有找到id为{stu_id}的学生信息!')
                            break
                else:
                    print('无学生信息')
                    break
                self.show_info()  #显示学生信息
                answer = input('是否继续删除? y/n\n')
                if answer == 'y' or answer == 'Y':
                    continue
                else:
                    break

    def modify(self):
        self.show_info()
        rwf = open(filename, 'r+', encoding='utf-8')
        stu_list = rwf.readlines()
        rwf.seek(0)     #重新将文件指针移至文件开头
        rwf.truncate()
        stu_id = input('请输入需要修改信息的学生id: ')
        modify_flag = False
        for it in stu_list:
            d = eval(it)
            if d['id'] == stu_id:
                modify_flag = True
                while True:
                    try:
                        d['name'] = input('请输入姓名: ')
                        d['english'] = int(input('请输入英语成绩: '))
                        d['python'] = int(input('请输入python绩: '))
                        d['java'] = int(input('请输入java绩: '))
                    except:
                        print('[modify]输入有误,请重新输入!')
                    else:
                        break
                rwf.write(str(d) + '\n')
                print('修改成功!')
            else:
                rwf.write(it)
        rwf.close()
        if not modify_flag:
            print('id为{0}的学生信息未找到!'.format(stu_id))
            return
        answer = input('是否继续修改? y/n\n')
        if answer == 'y' or answer == 'Y':
            self.modify()

    def search(self):
        search_stu_list = []
        search_flag = False
        choice = input('按ID查找输入1,按姓名查找输入2: ')
        if choice == '1':
            stu_id = input('请输入学生ID: ')
        elif choice == '2':
            stu_name = input('请输入学生姓名: ')
        else:
            print('[search]输入有误,请重新输入!')
            self.search()
        with open(filename, 'r', encoding='utf-8') as rf:
            stu_list = rf.readlines()
        for it in stu_list:
            it = dict(eval(it))
            if (choice == '1' and it.get('id') == stu_id) or (choice == '2' and it.get('name') == stu_name):
                search_stu_list.append(it)
                search_flag = True
        if search_flag:
            self.show_info(search_stu_list)
        else:
            print('未查到学生信息,请确认输入是否有误!')
        answer = input('是否继续查询? y/n\n')
        if answer == 'y' or answer == 'Y':
            self.search()

    @staticmethod
    def total():
        with open(filename, 'r', encoding='utf-8') as rf:
            stu_list = rf.readlines()
            if stu_list:
                print('一共有{}名学生'.format(len(stu_list)))
            else:
                print('还未录入学生信息!')

    def sort(self):
        stu_new_list = []
        self.show_info()
        with open(filename, 'r', encoding='utf-8') as rf:
            stu_list = rf.readlines()
        for it in stu_list:
            stu_new_list.append(eval(it))
        choice = input('请选择(0.升序 1.降序): ')
        if choice == '0':
            desc_order = False
        elif choice == '1':
            desc_order = True
        else:
            print('输入有误,请重新输入!')
            self.sort()
        mode = input('请输入排序方式(1.按英语成绩排序 2.按Python成绩排序 3.按Java成绩排序 0.按总成绩排序): ')
        if mode == '1':
            stu_new_list.sort(key=lambda val: int(val['english']), reverse=desc_order)  #lamada定义匿名函数
        elif mode == '2':
            stu_new_list.sort(key=lambda val: int(val['python']), reverse=desc_order)
        elif mode == '3':
            stu_new_list.sort(key=lambda val: int(val['java']), reverse=desc_order)
        elif mode == '0':
            stu_new_list.sort(key=lambda val: int(val['english']) + int(val['python']) + int(val['java']), reverse=desc_order)
        else:
            print('输入有误,请重新输入!')
            self.sort()
        with open(filename, 'w+', encoding='utf-8'):  #以w+方法打开文件以完成清空操作
            pass
        self.save(stu_new_list)
        print('排序完成!排序后学生信息如下:')
        self.show_info()


def main():
    stu_magsys = StuMagSys()

    while True:
        stu_magsys.menu()
        try:
            choice = int(input('请选择: '))
            if choice in range(8):
                if choice == 0:
                    answer = input('您确定要退出系统吗? y/n \n')
                    if answer == 'y' or answer == 'Y':
                        print('感谢您的使用!')
                        break
                    else:
                        continue
                elif choice == 1:
                    stu_magsys.insert()
                else:
                    if stu_magsys.isFileExist():
                        if choice == 2:
                            stu_magsys.search()
                        elif choice == 3:
                            stu_magsys.delete()
                        elif choice == 4:
                            stu_magsys.modify()
                        elif choice == 5:
                            stu_magsys.sort()
                        elif choice == 6:
                            stu_magsys.total()
                        elif choice == 7:
                            stu_magsys.show_info()
        except:
            traceback.print_exc()
            print('程序错误')


if __name__ == '__main__':
    main()

3 项目打包及演示

.py文件打包成exe文件(如何打包请参考pyinstaller项目打包成exe文件),然后运行exe文件,输入’7’查看当前系统的学生信息:

项目管理软件python源码 python管理系统项目_python_02


下面再演示下排序效果(其他功能不再演示):

项目管理软件python源码 python管理系统项目_项目管理软件python源码_03


END