python实现学生管理系统


Posted in Python onJanuary 11, 2018

python写的简单的学生管理系统,练习python语法。

可以运行在windows和linux下,python 2.7。

#!/usr/local/bin/python 
# -*- coding:utf-8 -*- 
 
import os 
import re 
 
#定义学生类 
class Student: 
 def __init__(self): 
  self.name = '' 
  self.ID = '' 
  self.score = 0 
 
#根据学生分数进行从大到小的冒泡排序 
def BuddleSortByScore( stulist ): 
 n = len( stulist ) 
 for i in range( n ): 
  for j in range( n - i - 1): 
   if stulist[j].score < stulist[j+1].score: 
    #tmp = stulist[j+1] 
    #stulist[j+1] = stulist[j] 
    #stulist[j] = tmp 
    stulist[j],stulist[j+1] = stulist[j+1],stulist[j] 
 
#按顺序输出所有学生的信息 
def PrintAllStudentInfo( stulist ): 
 print u"______________________学生列表_______________" 
 for i in range( len(stulist) ): 
  print u"姓名:" , 
  print stulist[i].name, 
  print " " , 
  print u"学号:" , 
  print stulist[i].ID , 
  print " " , 
  print u"分数:" , 
  print stulist[i].score 
 print "____________________________________________" 
 
#增加一个学生,增加成功返回True,否则返回False 
def Add( stulist , stu ): 
 if searchByID( stulist , stu.ID ) == 1: 
  print u"此ID已经存在!" 
  return False 
 stulist.append( stu ) 
 
 #给出是否保存更新数据的选择 
 print "Do you want to save the result ?" 
 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.score) ) 
  file_object.write( "\r\n" ) 
  file_object.close() 
  return True 
 else: 
  stulist.remove(stu) 
 
#根据ID删除一个学生的信息,删除成功则返回True,否则返回false 
def DeleteByID( stulist , ID ): 
 for stu in stulist: 
  if stu.ID == ID: 
   stulist.remove( stu ) 
 
   #选择是否保存已经改变的内容 
   print "Do you want to save the changed result ?" 
   nChoose = raw_input("Choose:Y/N:") 
    
   if nChoose == 'Y' or nChoose == 'y': 
    file_object = open("students.txt" , "w+") 
    for stu2 in stulist: 
     file_object.write(stu2.ID) 
     file_object.write(" ") 
     file_object.write(stu2.name) 
     file_object.write(" ") 
     file_object.write(str(stu2.score)) 
     file_object.write("\r\n") 
    file_object.close() 
    print u"删除成功!" 
   return True 
 print u"删除失败!" 
 return False 
 
 
#根据姓名删除一个学生的信息,删除成功返回True,否则返回False 
def DeleteByName( stulist , name ): 
 pos = searchByName( stulist , name ) 
 if pos != -1: 
  del stulist[pos] 
 
  #选择是否保存已经改变的内容 
  print "Do you want to save the changed result ?" 
  nChoose = raw_input("Choose:Y/N:") 
    
  if nChoose == 'Y' or nChoose == 'y': 
    file_object = open("students.txt" , "w+") 
    for stu2 in stulist: 
     file_object.write(stu2.ID) 
     file_object.write(" ") 
     file_object.write(stu2.name) 
     file_object.write(" ") 
     file_object.write(str(stu2.score)) 
     file_object.write("\r\n") 
    file_object.close() 
    print u"删除成功!" 
  return True 
 else: 
  print u"删除失败!" 
  print pos 
  return False 
 
 
#根据学号查找一个学生是否存在,存在返回学生在列表中的下标,否则返回-1 
def searchByID( stulist , ID ): 
 for i in range( len(stulist) ): 
  if stulist[i].ID == ID: 
   print u"姓名:" , 
   print stulist[i].name , 
   print " " , 
   print u"学号:" , 
   print stulist[i].ID , 
   print " " , 
   print u"分数:" , 
   print stulist[i].score 
   return i 
 return -1 
 
#根据姓名查找一个学生是否存在,存在返回学生在列表中的下标,否则返回-1 
def searchByName( stulist , name ): 
 for i in range( len(stulist) ): 
  if stulist[i].name == name: 
   print u"姓名:" , 
   print stulist[i].name , 
   print " " , 
   print u"学号:" , 
   print stulist[i].ID , 
   print " " , 
   print u"分数:" , 
   print stulist[i].score 
   return i 
 return -1 
 
#初始化,读取文件,更新学生信息 
def Init( stulist ): 
 print u"初始化......" 
  
 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 u"初始化成功!" 
       
 
#查找菜单 
def QueryMenu( stulist ): 
 while True: 
  print "******************************" 
  print u"根据学生的学号进行查找-------1" 
  print u"根据学生的姓名进行查找-------2" 
  print u"离开查找模块----------------3" 
  print "******************************" 
 
  nChoose = raw_input("请输入你的选择") 
 
  if nChoose == "1": 
   ID = raw_input("请输入你要输入查找的ID:") 
   searchByID( stulist , ID ) 
  elif nChoose == "2": 
   name = raw_input("请输入你要查找的姓名:") 
   searchByName( stulist , name ) 
  elif nChoose == "3": 
   return 
  else: 
   print u"选择输入错误,请重新输入!" 
    
#删除模块 
def DeleteMenu( stulist ): 
 while True: 
  print "*****************************" 
  print u"根据学生的学号进行删除------1" 
  print u"根据学生的姓名进行删除------2" 
  print u"离开删除模块---------------3" 
  print "******************************" 
 
  nChoose = raw_input("请进行选择:") 
 
  if nChoose == "1": 
   ID = raw_input("请输入你要删除的ID:") 
   DeleteByID( stulist , ID ) 
  elif nChoose == "2": 
   name = raw_input("请输入你要删除的姓名:") 
   DeleteByName( stulist , name ) 
  elif nChoose == "3": 
   return 
  else: 
   print u"您的选择有误,请重新输入!" 
    
       
#菜单 
def menu( stulist ): 
 while True: 
  print "***********************" 
  print u"--------菜单------------" 
  print u"增加学生信息---------1" 
  print u"查找一个学生的信息----2" 
  print u"删除一个学生的信息----3" 
  print u"输出所有学生的信息----4" 
  print u"根据分数排序---------5" 
  print u"退出程序-------------6" 
  print "------------------------" 
  print "************************" 
 
  nChoose = raw_input("请输入你的选择:") 
   
  if nChoose == "1": 
   stu = Student() 
   stu.name = raw_input("请输入学生的姓名:") 
 
   #匹配学生ID是否满足0--9中的数字 
   while True: 
    stu.ID = raw_input("请输入学生的ID:") 
    #创建编译一个正则表达式的模板 
    p = re.compile( '^[0-9]{3}$' ) 
    if p.match( stu.ID ): 
     break 
    else: 
     print "学生的ID输入错误,正确形式应该为0--9之间的三位数字!" 
 
   #匹配学生的分数是否满足0--100之间 
   while True:  
    stu.score = eval( raw_input("请输入学生的分数:") ) 
    #利用普通的符号来判断分数是否符合标准 
    #if stu.score >= 0 and stu.score <= 100: 
    # break 
    #利用正则表达式来判断分数是否符合标准 
    if re.match( "^[0-9]" ,str(stu.score) ) and stu.score<=100 and    stu.score >= 0 : 
     break 
    else: 
     print u"分数不满足实际情况,应该为0--100之间的数字!" 
 
   if Add( stulist , stu ) == 1: 
    print u"学生信息增加成功!" 
   else: 
    print u"学生信息增加失败!" 
  elif nChoose == "2": 
   QueryMenu( stulist ) 
  elif nChoose == "3": 
   DeleteMenu( stulist ) 
  elif nChoose == "4": 
   PrintAllStudentInfo( stulist ) 
  elif nChoose == "5": 
   BuddleSortByScore( stulist ) 
 
   print "Do you want to save the sorted result?" 
   choose = raw_input("please input your choice:Y/N:") 
   if choose == 'Y' or choose == 'y': 
    file_object = open("students.txt","w+") 
    for stu2 in stulist: 
     file_object.write(stu2.ID) 
     file_object.write(" ") 
     file_object.write(stu2.name) 
     file_object.write(" ") 
     file_object.write(str(stu2.score)) 
     file_object.write("\r\n") 
  elif nChoose == "6": 
   return 
  else: 
   print u"输入有误,请重新输入!" 
    
#测试函数部分 
if __name__ == '__main__': 
 #定义一个列表用来存储所有学生的信息 
 stulist = [] 
  
 #初始化,从文件中读取信息 
 Init( stulist ) 
  
 #菜单函数 
 menu( stulist )

运行需要读写文件Students.txt。文件格式如下图:

python实现学生管理系统

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

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

Python 相关文章推荐
使用Python开发windows GUI程序入门实例
Oct 23 Python
理解Python中的With语句
Feb 02 Python
Python栈类实例分析
Jun 15 Python
python实现给微信公众号发送消息的方法
Jun 30 Python
python+opencv轮廓检测代码解析
Jan 05 Python
python实现简易数码时钟
Feb 19 Python
NumPy 基本切片和索引的具体使用方法
Apr 24 Python
OpenCV 边缘检测
Jul 10 Python
Python综合应用名片管理系统案例详解
Jan 03 Python
python基于property()函数定义属性
Jan 22 Python
python tkinter的消息框模块(messagebox,simpledialog)
Nov 07 Python
Python机器学习三大件之一numpy
May 10 Python
linecache模块加载和缓存文件内容详解
Jan 11 #Python
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
Jan 11 #Python
python+django+sql学生信息管理后台开发
Jan 11 #Python
hmac模块生成加入了密钥的消息摘要详解
Jan 11 #Python
快速了解Python中的装饰器
Jan 11 #Python
简单了解python模块概念
Jan 11 #Python
100行Python代码实现自动抢火车票(附源码)
Jan 11 #Python
You might like
PHP 类型转换函数intval
2009/06/20 PHP
xss防御之php利用httponly防xss攻击
2014/03/21 PHP
PHP中通过trigger_error触发PHP错误示例
2015/06/23 PHP
thinkphp实现163、QQ邮箱收发邮件的方法
2015/12/18 PHP
PHP htmlspecialchars() 函数实例代码及用法大全
2018/09/18 PHP
用于table内容排序
2006/07/21 Javascript
nodejs实用示例 缩址还原
2010/12/28 NodeJs
JavaScript伸缩的菜单简单示例
2013/12/03 Javascript
jQuery之简单的表单验证实例
2016/07/07 Javascript
bootstrap table复杂操作代码
2016/11/01 Javascript
详解如何较好的使用js
2016/12/16 Javascript
JS中用try catch对代码运行的性能影响分析
2016/12/26 Javascript
详解JS中遍历语法的比较
2017/04/07 Javascript
hammer.js实现图片手势放大效果
2017/08/29 Javascript
浅谈React和Redux的连接react-redux
2017/12/04 Javascript
微信小程序实现天气预报功能
2018/07/18 Javascript
vue 优化CDN加速的方法示例
2018/09/19 Javascript
vue-quill-editor插入图片路径太长问题解决方法
2021/01/08 Vue.js
简单的Apache+FastCGI+Django配置指南
2015/07/22 Python
详解Python的Lambda函数与排序
2016/10/25 Python
Python爬虫实现网页信息抓取功能示例【URL与正则模块】
2017/05/18 Python
Python中max函数用于二维列表的实例
2018/04/03 Python
Python语法分析之字符串格式化
2019/06/13 Python
详解在python操作数据库中游标的使用方法
2019/11/12 Python
Python concurrent.futures模块使用实例
2019/12/24 Python
python sorted函数原理解析及练习
2020/02/10 Python
Python OpenCV读取中文路径图像的方法
2020/07/02 Python
将SVG图引入到HTML页面的实现
2019/09/20 HTML / CSS
英国蜡烛、蜡烛配件和家居香氛购买网站:Yankee Candle
2018/12/12 全球购物
预备党员思想汇报范文
2013/12/29 职场文书
酒店总经理欢迎词
2014/01/15 职场文书
中青班党性分析材料
2014/02/16 职场文书
美术毕业生求职信
2014/02/25 职场文书
2014年纪检监察工作总结
2014/11/11 职场文书
2016抗战胜利71周年红领巾广播稿
2015/12/18 职场文书
python画条形图的具体代码
2022/04/20 Python