基于python制作简易版学生信息管理系统


Posted in Python onApril 20, 2021

一、前言

本篇博客对于文件操作、字典、列表、匿名函数以及sort()等内置函数进行了系统的整理操作,以设计一个学生信息管理系统的形式展示,具体概念方法等会在代码后进行分析讲述,请读者仔细分析每一处解析,对于基础巩固将会有很大的帮助,其中还有每一块代码的设计思路图,逻辑分析会有一定的提升。

二、需求分析

基于python制作简易版学生信息管理系统

基于python制作简易版学生信息管理系统

本程序需要用到os模板首先导入,并命名要存储信息的文件

import os
File_Object_Name = 'Student_Inforation.txt'

三、主函数

基于python制作简易版学生信息管理系统

def Main():
    while True:
        Menu()
        _Select = int(input('Please select operation:'))
        if _Select in [0, 1, 2, 3, 4, 5, 6, 7]:
            if _Select == 0:
                _Answer = input('Do you want to log out?(Y/N)')
                if _Answer == 'Y' or _Answer == 'y':
                    print('Thank for you use!!!')
                    break
                    pass
                else:
                    continue
                    pass
                pass
            elif _Select == 1:
                Insert_Infor()
                pass
            elif _Select == 2:
                Search_Infor()
                pass
            elif _Select == 3:
                Delete_Infor()
                pass
            elif _Select == 4:
                Change_Infor()
                pass
            elif _Select == 5:
                Sort()
                pass
            elif _Select == 6:
                Count_Total_Num()
                pass
            elif _Select == 7:
                Show_Infor()
                pass
            pass
        else:
            print('Error Input!!!')
            pass

四、功能菜单

def Menu():
    print('=========Student Information Management System=========')
    print('---------------------Function menu---------------------')
    print('             1、Input Student Information')
    print('             2、Search Student Information')
    print('             3、Delete Student Information')
    print('             4、Change Student Information')
    print('             5、Sort According to Score')
    print('             6、Count Total Student Number')
    print('             7、Show All Student Information')
    print('             0、Log Out')
    print('-------------------------------------------------------')
    pass

五、录入信息

基于python制作简易版学生信息管理系统

def Insert_Infor():
    Student_Infor_List = []  # 创建一个学生信息空列表,后面会用到
    while True:
        Stu_Id = input('Please input the id(such as 1001,1002):')
        if not Stu_Id:  # 空的字符串转为bool类型为False,非空为True,此处若是输入为空会直接跳出循环。(空列表、字典、元组等都满足)
            break
            pass
        Stu_Name = input('Please input the name:')
        if not Stu_Name:
            break
            pass
        try:
            English_Score = int(input('Please input the English score:'))
            Python_Score = int(input('Please input the Python score:'))
            Java_Score = int(input('Please input the Java score:'))
            pass
        except:
            print('Error Input(not int),please input again')
            continue
            pass
        # 将每个学生的信息放入一个字典当中
        Student_Infor_Dict = {'Id': Stu_Id, 'Name': Stu_Name, 'English': English_Score, 'Python': Python_Score, 'Java': Java_Score}
        # 将一个字典作为一个元素放入学生信息列表
        Student_Infor_List.append(Student_Infor_Dict)  # 使用 append() 方法来添加列表项
        _Answer = input('Whether to input more?(Y/N)')
        if _Answer == 'Y' or _Answer == 'y':
            continue
            pass
        else:
            break
            pass
        pass
    # 将学生信息列表中的字典元素保存到文件之中,调用Keep_Infor()函数
    Keep_Infor(Student_Infor_List)
    print('Input Finished')
    pass

六、保存信息

def Keep_Infor(List):
    File_Object = open(File_Object_Name, mode='a', encoding='utf-8')
    # 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
    for item in List:
        File_Object.write(str(item)+'\n')
        pass
    # 将传入的列表中的字典元素强制转换成字符串类型并拼接上换行符写入文件
    File_Object.close()

七、查找信息

基于python制作简易版学生信息管理系统

def Search_Infor():
    Student_Query = [] # 空列表,之后用于存放查找到的学生信息
    while True:
        Student_Id = ''
        Student_Name = ''
        if os.path.exists(File_Object_Name):
        # os.path模块主要用于文件的属性获取,os.path.exists()就是判断括号里的文件是否存在,括号内的可以是文件路径/名。
            Search_Mode = int(input('search by id(1) or by name(2):'))
            if Search_Mode == 1:
                Student_Id = input('Please input id:')
                pass
            elif Search_Mode == 2:
                Student_Name = input('Please input name:')
                pass
            else:
                print('Error Input,try again!')
                Search_Infor()
                pass
            with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:  # 只读模式打开文件并将类对象赋给File_Object
                Student_List = File_Object.readlines()  # readlines()按行读取,一次性读取所有内容,返回一个列表,每一行内容作为一个元素
                for item in Student_List:  # 遍历列表中元素
                    _Dict = eval(item)
                    # 由于是以字符串类型保存到文件中,此时item相当于'dict'形式,要将其用eval()函数转换为字典,即脱掉''。
                    if Student_Id:  # 如果选的是按照姓名查找,则此空字符串已经不再为空,bool类型为True
                        if Student_Id == _Dict['Id']:
                            Student_Query.append(_Dict)
                            pass
                        pass
                    elif Student_Name:
                        if Student_Name == _Dict['Name']:
                            Student_Query.append(_Dict)
                            pass
                        pass
                    pass
                if Student_Query == []:  # 仍为空说明未找到。
                    print('no such student,try again')
                    Search_Infor()  # 再次调用函数
                    pass
                pass
            pass
        else:
            print('Still no such file to keep student information!')
            pass
        print(Student_Query[0])  # 此时该列表中只有一个字典元素,直接打印输出
        Student_Query.clear()  # 调用内置函数清空列表,方便下次使用
        _Answer = input('Whether to search others?(Y/N)')
        if _Answer == 'Y' or _Answer == 'y':
            continue
            pass
        else:
            return
        pass
    pass

八、删除信息

基于python制作简易版学生信息管理系统

def Delete_Infor():
    while True:
        Student_Id = input('Please input the student‘s id that you want to delete:')
        if Student_Id:
            if os.path.exists(File_Object_Name):
                with open(File_Object_Name, 'r', encoding='utf-8')as File1:
                    Old_Student_Infor = File1.readlines()  # 读取每行作为元素放入Old_Student_Infor列表
                    pass
                pass
            else:
                Old_Student_Infor = []
                pass
            Flag = False  # 是否删除成功的标志
            if Old_Student_Infor:
                with open(File_Object_Name, 'w', encoding='utf-8')as File2:
                    _Dict = {}
                    for item in Old_Student_Infor:
                        _Dict = eval(item)  # 将删除信息之前的列表元素转化为字典形式赋给_Dict
                        if _Dict['Id'] != Student_Id:
                            File2.write(str(_Dict)+'\n')
                            # 如果与要删除的信息的Id不同,覆盖写入原文件
                            pass
                        else:
                            Flag = True  # 如果相同的话,则不写入文件中,相当于删除成功
                            pass
                        pass
                    if Flag:
                        print('Student information of {} has been delete'.format(Student_Id))
                        pass
                    else:
                        print('Can not find student of id:{}'.format(Student_Id))
                        pass
                    pass
                pass
            else:
                print('file have no student information')
                break
                pass
            Show_Infor()
            _Answer = input('Whether to delete more?(Y/N)')
            pass
        if _Answer == 'Y' or _Answer == 'y':
            continue
            pass
        else:
            break
            pass
        pass
    pass

九、修改信息

基于python制作简易版学生信息管理系统

def Change_Infor():
    Show_Infor()
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as old_file:
            Old_Student_Infor = old_file.readlines()
            pass
        pass
    else:
        return
    Student_Id = input('Please input the id you wanna change:')
    if Student_Id:
        with open(File_Object_Name, 'w', encoding='utf-8')as new_file:
            for item in Old_Student_Infor:
                _dict = dict(eval(item))
                if _dict['Id'] == Student_Id:
                    print('Find it,can change it!')
                    while True:
                        try:
                            _dict['Name'] = input('Please input new name:')
                            _dict['English'] = input('Please input new English score:')
                            _dict['Python'] = input('Please input new Python score:')
                            _dict['Java'] = input('Please input new Java score:')
                            pass
                        except:
                            print('Error Input!!!Try again')
                            pass
                        new_file.write(str(_dict)+'\n')
                        print('Successfully change!')
                        break
                        pass
                    pass
                else:
                    print('Can‘t find it')
                    new_file.write(str(_dict)+'\n')
                    pass
                pass
            pass
        pass
    _Answer = input('Whether to change more?(Y/N)')
    if _Answer == 'y' or _Answer == 'Y':
        Change_Infor()
        pass
    else:
        return
    pass

十、显示信息

基于python制作简易版学生信息管理系统

def Show_Infor():
    Infor_List = []
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:
            Stu_List = File_Object.readlines()
            for item1 in Stu_List:
                Infor_List.append(dict(eval(item1)))
                pass
            if Infor_List:
                for item2 in Infor_List:
                    print(item2)
                    pass
                pass
            else:
                print('no student')
                pass
            pass
        pass
    else:
        print('no such file')
        pass
    pass

十一、按成绩排序

基于python制作简易版学生信息管理系统

def Sort():
    Show_Infor()
    Infor_List = []
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:
            Student_List = File_Object.readlines()
            for item in Student_List:
                _Dict = dict(eval(item))
                Infor_List.append(_Dict)
                pass
            pass
        pass
    else:
        print('no such file')
        return
    Sort_Mode = bool(input('Please input sort mode(0、ascending order|1、descending order)'))
    if not Sort_Mode:  # ascending order
        reverse_mode = True
        pass
    else:  # descending order
        reverse_mode = False
        pass
    Specific_Sort_Mode = int(input('Sort by English(1),by Python(2),by Jave(3),by total(4):'))
    if Specific_Sort_Mode == 1:
        Infor_List.sort(key=lambda x: int(x['English']), reverse=reverse_mode)
        pass
    elif Specific_Sort_Mode == 2:
        Infor_List.sort(key=lambda x: int(x['Python']), reverse=reverse_mode)
        pass
    elif Specific_Sort_Mode == 3:
        Infor_List.sort(key=lambda x: int(x['Java']), reverse=reverse_mode)
        pass
    elif Specific_Sort_Mode == 4:
        Infor_List.sort(key=lambda x: int(x['English']) + int(x['Python']) + int(x['Java']), reverse=reverse_mode)
        pass
    else:
        print('Error Input,try again')
        Sort()
        pass
    for item in Infor_List:
        print(item)
        pass
    pass
  • sort()函数原型: list.sort(key=None, reverse=False)

key参数 :
key接受的是一个只有一个形参的函数,形式如下
def f(a):
return len(a)
key接受的函数返回值,表示此元素的权值,sort将按照权值大小进行排序
reverse参数 :
reverse接受的是一个bool类型的值 (Ture or False),表示是否颠倒排列顺序,一般默认的是False

十二、统计人数

基于python制作简易版学生信息管理系统

def Count_Total_Num():
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:
            Stu_List = File_Object.readlines()
            _Count = len(Stu_List)
            if _Count >= 2:
                print('A total of {} students')
                pass
            elif _Count ==1:
                print('A single student!')
                pass
            else:
                print('no student!')
                pass
            pass
        pass
    else:
        print('still no such file!')
        pass
    pass

十三、最后记得运行主函数

Main()

十四、将程序打包成可执行exe文件

1.cmd进入command界面

2.输入pip install PyInstaller

3.安装完成后仍在cmd界面输入pyinstaller -F py为扩展名的文件路径\文件名.py

4.操作后将会有一大串代码,倒数第二行会有最终文件的保存地址,打开之后将之前编译程序生成的txt文件移入该文件夹中即可操作

到此这篇关于基于python制作简易版学生信息管理系统的文章就介绍到这了,更多相关python学生信息管理系统内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python3中多线程编程的队列运作示例
Apr 16 Python
浅谈Python中copy()方法的使用
May 21 Python
python显示生日是星期几的方法
May 27 Python
Python+selenium实现截图图片并保存截取的图片
Jan 05 Python
pycharm设置注释颜色的方法
May 23 Python
python生成1行四列全2矩阵的方法
Aug 04 Python
python-opencv颜色提取分割方法
Dec 08 Python
python利用ffmpeg进行录制屏幕的方法
Jan 10 Python
numpy数组广播的机制
Jul 12 Python
TFRecord格式存储数据与队列读取实例
Jan 21 Python
基于python 等频分箱qcut问题的解决
Mar 03 Python
Python实现JS解密并爬取某音漫客网站
Oct 23 Python
基于python的matplotlib制作双Y轴图
90行Python代码开发个人云盘应用
基于python实现银行管理系统
python爬虫框架feapde的使用简介
Apr 20 #Python
python实现大文本文件分割成多个小文件
Apr 20 #Python
Python绘制分类图的方法
Pytest allure 命令行参数的使用
You might like
discuz免激活同步登入代码修改方法(discuz同步登录)
2013/12/24 PHP
PHP中is_file()函数使用指南
2015/05/08 PHP
ThinkPHP控制器详解
2015/07/27 PHP
CodeIgniter连贯操作的底层原理分析
2016/05/17 PHP
IIS 7.5 asp Session超时时间设置方法
2017/04/17 PHP
PHP面向对象之工作单元(实例讲解)
2017/06/26 PHP
PHP长连接实现与使用方法详解
2018/02/11 PHP
浅析PHP数据导出知识点
2018/02/17 PHP
Javascript 验证上传图片大小[客户端]
2009/08/01 Javascript
JS 加入收藏夹的代码(主流浏览器通用)
2013/05/13 Javascript
Js日期选择自动填充到输入框(界面漂亮兼容火狐)
2013/08/02 Javascript
Dojo Javascript 编程规范 规范自己的JavaScript书写
2014/10/26 Javascript
深入理解JavaScript系列(17):面向对象编程之概论详细介绍
2015/03/04 Javascript
HTML5使用DeviceOrientation实现摇一摇功能
2015/06/05 Javascript
JS清除文本框内容离开在恢复及鼠标离开文本框时触发js的方法
2016/01/12 Javascript
AngularJs 60分钟入门基础教程
2016/04/03 Javascript
js将json格式的对象拼接成复杂的url参数方法
2016/05/25 Javascript
JavaScript中的boolean布尔值使用学习及相关技巧讲解
2016/05/26 Javascript
浅谈移动端之js touch事件 手势滑动事件
2016/11/07 Javascript
JavaScript组件开发之输入框加候选框
2017/03/10 Javascript
jQuery插件FusionWidgets实现的AngularGauge图效果示例【附demo源码】
2017/03/23 jQuery
Javascript中将变量转换为字符串的三种方法
2017/09/19 Javascript
详解vue项目打包后通过百度的BAE发布到网上的流程
2018/03/05 Javascript
javascrpt密码强度校验函数详解
2020/03/18 Javascript
[01:38:19]夜魇凡尔赛茶话会 第五期
2021/03/11 DOTA
python操作MongoDB基础知识
2013/11/01 Python
python抓取京东价格分析京东商品价格走势
2014/01/09 Python
Python中MySQLdb和torndb模块对MySQL的断连问题处理
2015/11/09 Python
python 实现调用子文件下的模块方法
2018/12/07 Python
使用APScheduler3.0.1 实现定时任务的方法
2019/07/22 Python
python 发送json数据操作实例分析
2019/10/15 Python
HTML5新特性之用SVG绘制微信logo
2016/02/03 HTML / CSS
机械设计专业应届生求职信
2013/11/21 职场文书
志愿者工作心得体会
2016/01/15 职场文书
导游词之茶卡盐湖
2019/11/26 职场文书
MySql子查询IN的执行和优化的实现
2021/08/02 MySQL