Python实现学生成绩管理系统


Posted in Python onApril 05, 2020

本文实例为大家分享了Python实现学生成绩管理系统的具体代码,供大家参考,具体内容如下

基本功能:

输入并存储学生的信息:通过输入学生的学号、姓名、和分数,然后就可以把数据保存在建立的student文件里面。

打印学生的所有信息:通过一个打印函数就可以把所有的信息打印在屏幕上。

修改学生信息:这个功能首先通过查询功能查询出该学生是否存在,如果存在就对该学生的信息进行修改,如果不存在则返回到主界面。

删除学生信息:该功能是对相应的学生进行删除操作,如果学生存在就查找到进行删除。

按学生成绩进行排序: 这个功能是按照学生的成绩进行排序,对学生的信息进行操作。

查找学生信息:这个功能通过输入学号,查找该学生的信息,如果有该学号就输出该学生的信息,没有该学号就提示输入的学号不存在。

初始化功能

系统在开始使用之前先进行初始化功能,判断students.txt文件中是否保存的有学生的信息,如果有就把文件的内容读取出来,供接下来的操作使用,如用没有就初始化一个空的列表,用来保存用户的输入,程序中接下来的所有数据都会保存在该列表中相当与一个数据缓冲区。

首先是打开文件操作,对文件中的内容进行读取操作,由于在文件中保存的内容是由空格进行分割的,并且每一个学生的信息都占用一行,首先读出所有的内容,先进行按照换行进行分割,得到每个人的信息,然后再对每个人的信息进行安装空格分隔,得到每个人的详细信息包括用户的姓名,学号,成绩。

def Init(stulist): #初始化函数 
 print "初始化......" 
 file_object = open('students.txt', 'r') 
 for line in file_object: 
 stu = Student() 
 line = line.strip("\n") 
 s = line.split(" ") 
 stu.ID = s[0] 
 stu.name = s[1] 
 stu.score = s[2] 
 stulist.append(stu) 
print "初始化成功!"

成绩排序实现

这部分代码是按照学生成绩的高低进行排序,在实现的时候,首先是把所有人的成绩放到一个列表里面然后使用插入排序,按照成绩的大小对StuList中保存的学生信息的地址进行排序

def Sort(stulist): #按学生成绩排序
 Stu = []
 sum_count = []
 for li in stulist:
 temp = []
 temp.append(li.ID)
 temp.append(li.name)
 temp.append(int(li.score1))
 temp.append(int(li.score2))
 temp.append(int(li.score3))
 temp.append(int(li.sum))
 sum_count.append(int(li.sum))
 Stu.append(temp)

 #print sum_count
 #print Stu;
 #print stulist
 insertSort(sum_count, stulist)
 #print stulist;
 display(stulist)

def insertSort(a, stulist): 
 for i in range(len(a)-1): 
 #print a,i 
 for j in range(i+1,len(a)): 
 if a[i]<a[j]: 
 temp = stulist[i] 
 stulist[i] = stulist[j] 
 stulist[j] = temp

界面截图如下:

Python实现学生成绩管理系统

源码:

# -*- coding: UTF-8 -*-

import os
import re
import numpy as np

class Student: #定义一个学生类
 def __init__(self):
 self.name = ''
 self.ID =''
 self.score1 = 0
 self.score2 = 0
 self.score1 = 0
 self.sum = 0


def searchByID(stulist, ID): #按学号查找看是否学号已经存在
 for item in stulist:
 if item.ID == ID:
 return True

def Add(stulist,stu): #添加一个学生信息
 if searchByID(stulist, stu.ID) == True:
 print"学号已经存在!"
 return False
 stulist.append(stu)
 print stu.name,stu.ID, stu.score1, stu.score2, stu.score3, stu.sum;
 print "是否要保存学生信息?"
 nChoose = raw_input("Choose Y/N")
 if nChoose == 'Y' or nChoose == 'y':
 file_object = open("students.txt", "a")
 file_object.write(stu.ID)
 file_object.write(" ")
 file_object.write(stu.name)
 file_object.write(" ")
 file_object.write(str(stu.score1))
 file_object.write(" ")
 file_object.write(str(stu.score2))
 file_object.write(" ")
 file_object.write(str(stu.score3))
 file_object.write(" ")
 file_object.write(str(stu.sum))
 file_object.write("\n")
 file_object.close()
 print u"保存成功!"

def Search(stulist, ID): #搜索一个学生信息
 print u"学号 姓名 语文 数学 英语 总分"
 count = 0
 for item in stulist:
 if item.ID == ID:
 print item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum
 break
 count = 0
 if count == len(stulist):
 print "没有该学生学号!"

def Del(stulist, ID): #删除一个学生信息
 count = 0
 for item in stulist:
 if item.ID == ID:
 stulist.remove(item)
 print "删除成功!"
 break
 count +=1
 # if count == len(stulist):
 # print "没有该学生学号!"
 file_object = open("students.txt", "w")
 for stu in stulist:
 print stu.ID, stu.name, stu.score1,stu.score2, stu.score3, stu.sum
 file_object.write(stu.ID)
 file_object.write(" ")
 file_object.write(stu.name)
 file_object.write(" ")
 file_object.write(str(stu.score1))
 file_object.write(" ")
 file_object.write(str(stu.score2))
 file_object.write(" ")
 file_object.write(str(stu.score3))
 file_object.write(" ")
 file_object.write(str(stu.sum))
 file_object.write("\n")
 file_object.close()
 # print "保存成功!"
 file_object.close()
def Change(stulist, ID):
 count = 0
 for item in stulist:
 if item.ID == ID:
 stulist.remove(item)
 file_object = open("students.txt", "w")
 for stu in stulist:
 #print li.ID, li.name, li.score
 file_object.write(stu.ID)
 file_object.write(" ")
 file_object.write(stu.name)
 file_object.write(" ")
 file_object.write(str(stu.score1))
 file_object.write(" ")
 file_object.write(str(stu.score2))
 file_object.write(" ")
 file_object.write(str(stu.score3))
 file_object.write(" ")
 file_object.write(str(stu.sum))
 file_object.write("\n")
 # print "保存成功!"
 file_object.close()
 stu = Student()
 stu.name = raw_input("请输入学生的姓名")
 while True:
 stu.ID = raw_input("请输入学生的ID")
 p = re.compile('^[0-9]{3}$')
 if p.match(stu.ID):
 break
 else:
 print "输入的有错误!"
 while True:
 stu.score1 = int(raw_input("请输入学生语文成绩"))
 if stu.score1 <= 100 and stu.score1 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score2 = int(raw_input("请输入学生数学成绩"))
 if stu.score2 <= 100 and stu.score2 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score3 = int(raw_input("请输入学生英语成绩"))
 if stu.score3 <= 100 and stu.score3 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 stu.sum = stu.score1 + stu.score2 + stu.score3
 Add(stulist,stu)
def display(stulist): #显示所有学生信息
 print u"学号 姓名 语文 数学 英语 总分"
 for item in stulist:
 print item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum

def Sort(stulist): #按学生成绩排序
 Stu = []
 sum_count = []
 for li in stulist:
 temp = []
 temp.append(li.ID)
 temp.append(li.name)
 temp.append(int(li.score1))
 temp.append(int(li.score2))
 temp.append(int(li.score3))
 temp.append(int(li.sum))
 sum_count.append(int(li.sum))
 Stu.append(temp)

 #print sum_count
 #print Stu;
 #print stulist
 insertSort(sum_count, stulist)
 #print stulist;
 display(stulist)

def insertSort(a, stulist): 
 for i in range(len(a)-1): 
 #print a,i 
 for j in range(i+1,len(a)): 
 if a[i]<a[j]: 
 temp = stulist[i] 
 stulist[i] = stulist[j] 
 stulist[j] = temp 
 #return a 

def Init(stulist): #初始化函数
 print "初始化......"
 file_object = open('students.txt', 'r')
 for line in file_object:
 stu = Student()
 line = line.strip("\n")
 s = line.split(" ")
 stu.ID = s[0]
 stu.name = s[1]
 stu.score1 = s[2]
 stu.score2 = s[3]
 stu.score3 = s[4]
 stu.sum = s[5]
 stulist.append(stu)
 file_object.close()
 print "初始化成功!"
 main()

def main(): #主函数 该程序的入口函数
 while True:
 print "*********************"
 print u"--------菜单---------"
 print u"增加学生信息--------1"
 print u"查找学生信息--------2"
 print u"删除学生信息--------3"
 print u"修改学生信息--------4"
 print u"所有学生信息--------5"
 print u"按照分数排序--------6"
 print u"退出程序------------0"
 print "*********************"

 nChoose = raw_input("请输入你的选择:")
 if nChoose == "1":
 stu = Student()
 stu.name = raw_input("请输入学生的姓名")
 while True:
 stu.ID = raw_input("请输入学生的ID")
 p = re.compile('^[0-9]{3}$')
 if p.match(stu.ID):
 break
 else:
 print "输入的有错误!"
 while True:
 stu.score1 = int(raw_input("请输入学生语文成绩"))
 if stu.score1 <= 100 and stu.score1 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score2 = int(raw_input("请输入学生数学成绩"))
 if stu.score2 <= 100 and stu.score2 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score3 = int(raw_input("请输入学生英语成绩"))
 if stu.score3 <= 100 and stu.score3 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 stu.sum = stu.score1 + stu.score2 + stu.score3
 Add(stulist,stu)

 if nChoose == '2':
 ID = raw_input("请输入学生的ID")
 Search(stulist, ID)

 if nChoose == '3':
 ID = raw_input("请输入学生的ID")
 Del(stulist, ID)
 if nChoose == '4':
 ID = raw_input("请输入学生的ID")
 Change(stulist, ID)

 if nChoose == '5':
 display(stulist)

 if nChoose == '6':
 Sort(stulist)


 if nChoose == '0':
 break

if __name__ == '__main__':
 stulist =[]
Init(stulist)

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网络编程之读取网站根目录实例
Sep 30 Python
python实现逆波兰计算表达式实例详解
May 06 Python
Django内容增加富文本功能的实例
Oct 17 Python
python编程使用selenium模拟登陆淘宝实例代码
Jan 25 Python
python 实现对文件夹内的文件排序编号
Apr 12 Python
python3中zip()函数使用详解
Jun 29 Python
Python向excel中写入数据的方法
May 05 Python
jupyter 实现notebook中显示完整的行和列
Apr 09 Python
如何基于windows实现python定时爬虫
May 01 Python
python3实现简单飞机大战
Nov 29 Python
一文带你了解Python 四种常见基础爬虫方法介绍
Dec 04 Python
Django搭建项目实战与避坑细节详解
Dec 06 Python
名片管理系统python版
Jan 11 #Python
Python学生成绩管理系统简洁版
Apr 05 #Python
Python实现学校管理系统
Jan 11 #Python
Python实现GUI学生信息管理系统
Apr 05 #Python
使用python 爬虫抓站的一些技巧总结
Jan 10 #Python
python版学生管理系统
Jan 10 #Python
python实现用户管理系统
Jan 10 #Python
You might like
php之CodeIgniter学习笔记
2013/06/17 PHP
使用PHP编写的SVN类
2013/07/18 PHP
PHP编程中的常见漏洞和代码实例
2014/08/06 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
2015/04/07 PHP
WordPres对前端页面调试时的两个PHP函数使用小技巧
2015/12/22 PHP
php 输出json及显示json中的中文汉字详解及实例
2016/11/09 PHP
可以显示单图片,多图片ajax请求的ThickBox3.1类下载
2007/12/23 Javascript
JavaScript对象模型-执行模型
2008/04/28 Javascript
jquery 操作日期、星期、元素的追加的实现代码
2012/02/07 Javascript
js防止表单重复提交实现代码
2012/09/05 Javascript
JavaScript实现点击按钮切换网页背景色的方法
2015/10/17 Javascript
Javascript 6里的4个新语法
2016/08/25 Javascript
关于Jquery中的bind(),on()绑定事件方式总结
2016/10/26 Javascript
十大 Node.js 的 Web 框架(快速提升工作效率)
2017/06/30 Javascript
nodejs 简单实现动态html的方法
2018/05/12 NodeJs
JavaScript设计模式之代理模式实例分析
2019/01/16 Javascript
Node.js Stream ondata触发时机与顺序的探索
2019/03/08 Javascript
Python判断文件和字符串编码类型的实例
2017/12/21 Python
win7 x64系统中安装Scrapy的方法
2018/11/18 Python
关于pymysql模块的使用以及代码详解
2019/09/01 Python
Python+OpenCV实现旋转文本校正方式
2020/01/09 Python
Jupyter notebook如何修改平台字体
2020/05/13 Python
python pymysql链接数据库查询结果转为Dataframe实例
2020/06/05 Python
No module named ‘win32gui‘ 的解决方法(踩坑之旅)
2021/02/18 Python
HTML5触摸事件(touchstart、touchmove和touchend)的实现
2020/05/08 HTML / CSS
高山背包:High Sierra
2017/11/23 全球购物
澳大利亚男士西服品牌:M.J.Bale
2018/02/06 全球购物
加拿大户外探险购物网站:SAIL
2020/06/27 全球购物
文秘人员工作职责
2014/01/31 职场文书
个人充满哲理的自我评价
2014/02/20 职场文书
三八妇女节活动主持词
2014/03/17 职场文书
2014年度安全生产目标管理责任书
2014/07/25 职场文书
户外活动总结
2015/02/04 职场文书
2015年毕业生实习评语
2015/03/25 职场文书
小升初自荐信怎么写
2015/03/26 职场文书
Ruby GDBM操作简介及数据存储原理
2022/04/19 Ruby