python学生管理系统代码实现


Posted in Python onApril 05, 2020

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

class Student: 
 stuID = "" 
 name = "" 
 sex = "M" 
 classID = "NULL" 
 
 #set ID 
 def setStuID(self,stuID): 
 self.stuID = stuID 
 
 def setName(self,name): 
 self.name = name 
 
 def setSex(self,sex): 
 self.sex = sex 
  
 def setClassID(self,classID): 
 self.classID = classID 
 
 def getStuId(self): 
 return self.stuID 
 
 def getName(self): 
 return self.name 
 
 def getSex(self): 
 return self.sex 
 
 def getClassID(self): 
 return self.classID

主函数

import os 
import re 
import sys 
import string 
import student 
 
#save file 
global FILEPATH 
FILEPATH = "student.db" 
#temporary file 
global TEMPFILE 
TEMPFILE = "temp.db" 
 
#This is menu 
def menu(): 
 while True : 
 print "1.Add a student information" 
 print "2.Query student information" 
 print "3.Delete a student information" 
 print "0.Quit" 
 opt = raw_input("Select:") 
 if opt == "1": 
  while True: 
  addStudent() 
  opt2 = raw_input("Continue Add(Y/N)?:") 
  if opt2 == "Y" or opt2 == "y" or opt2 == "": 
   continue 
  else: 
   break 
 elif opt == "2": 
  while True: 
  query() 
  opt2 = raw_input("Continue Query(Y/N)?:") 
  if opt2 == "Y" or opt2 == "y" or opt2 == "": 
   continue 
  else: 
   break  
 elif opt == "3": 
  while True: 
  delMenu() 
  opt2 = raw_input("Continue Delete(Y/N)?:") 
  if opt2 == "Y" or opt2 == "y" or opt2 == "": 
   continue 
  else: 
   break  
 elif opt == "0" : 
  exitProgram() 
  break 
 else: 
  print "Error input" 
   
#Add a student 
def addStudent() : 
 stu = student.Student() 
 while True: 
 stuID = raw_input("ID(001-999):") 
 #match ID 001-999 
 p = re.match("^[0-9]{3}$", stuID) 
 if p : 
  if stuID == "000": 
  print "ID must be 001-999" 
  continue 
  if isIDExist(stuID): 
  print "ID = %s already exist!" % stuID 
  continue 
  else : 
  stu.setStuID(stuID) 
  break 
 else: 
  print "ID must be 001-999" 
 
 while True:  
 stuName = raw_input("Name(a-z,A-Z,5 char):") 
 #match name a-z A-Z 5 char 
 p = re.match("^[a-zA-Z]{1,5}$",stuName) 
 if p : 
  stu.setName(stuName) 
  break 
 else : 
  print "Name format error must a-z,A-Z,within 5 char" 
 
 while True: 
 stuSex = raw_input("Sex(default is M):") 
 #default value 
 if stuSex == "": 
  stu.setSex("M") 
  print "Sex:M" 
  break 
 if stuSex =="M" or stuSex == "m": 
  stu.setSex(string.upper(stuSex)) 
  break  
 #if stuSex == "M" or stuSex == "m" || stuSex == "F" stuSex == "f": 
 p = re.match("^M|m|F|f$",stuSex) 
 if p : 
  stu.setSex(string.upper(stuSex)) 
  break 
 else : 
  print "Sex(M/f)" 
  
 while True: 
 stuClass = raw_input("Class(01-99):") 
 #default value 
 if stuClass == "": 
  stu.setClassID("NULL") 
  print "CLASS:NULL" 
  break 
 #match 00-99 
 p = re.match("^[0-9]{2}$",stuClass) 
 if p : 
  #get rid of 00 
  if stuClass == "00": 
  print "Class must 01-99" 
  continue 
  stu.setClassID(stuClass) 
  break 
 else: 
  print "Class must 01-99" 
 #save to file  
 file1 = open(FILEPATH,"a") 
 print "ID\tNAME\tSEX\tCLASS" 
 print stu.getStuId(),"\t",stu.getName(),"\t",stu.getSex(),"\t",stu.getClassID() 
 file1.write(stu.getStuId()+"\t"+stu.getName()+"\t"+stu.getSex()+"\t"+stu.getClassID()+"\n") 
 print "Add student success!" 
 file1.close() 
 
#Delete student menu 
def delMenu(): 
 print "1.Delete by ID" 
 print "2.Delete contains ID" 
 opt = raw_input("Select:") 
 if opt == "1": 
 delStudentByID() 
 elif opt == "2": 
 delStudentContainsID() 
 else: 
 print "Error input" 
  
#Delete contains id  
def delStudentContainsID(): 
 contID = raw_input("ID:") 
 if getInfoContainsID(contID)==0 : 
 print "Can't find ID contains \"%s\" student" % contID 
 return 
 opt = raw_input("Are you sure delete all(Y/N):") 
 if not opt == "y" or opt == "Y": 
 return 
 f = open(FILEPATH,"r") 
 tmp = open(TEMPFILE,"a") 
 i=0 
 for eachLine in f: 
 items = eachLine.split("\t") 
# if not re.match(contID, items[0]): 
 if items[0].count(contID) ==0: 
  tmp.write(eachLine) 
 else: 
  i+=1 
 f.close() 
 tmp.close() 
 os.remove(FILEPATH) 
 os.rename(TEMPFILE, FILEPATH) 
 print "Deleted %d data" % i 
 
#get contains ID information   
def getInfoContainsID(stuID): 
 f = open(FILEPATH) 
 i=0 
 for eachLine in f: 
 items = eachLine.split("\t") 
  
 if not items[0].count(stuID) ==0: 
# if re.match(stuID,items[0]): 
  i+=1 
  if i==1: 
  print "ID\tNAME\tSEX\tCLASS" 
  print eachLine, 
 if i==0: 
 return 0 
 else : 
 return i 
 f.close() 
  
#Delete student by ID 
def delStudentByID(): 
 delID = raw_input("Delete ID:") 
 if not isIDExist(delID) : 
 print "Can't find ID = %s student information" % delID 
 return 
 
 getInfoByID(delID) 
 opt = raw_input("Are you sure delete it(Y/N):") 
 if not (opt == "Y" or opt == "y"): 
 return 
 
 f = open(FILEPATH,"r") 
 tmp = open(TEMPFILE,"a") 
 
 for eachLine in f: 
 split = eachLine.split("\t")  
 if not delID == split[0]: 
  tmp.write(eachLine)   
 tmp.close() 
 f.close() 
 os.remove(FILEPATH) 
 os.rename(TEMPFILE, FILEPATH) 
 print "Delete success!" 
 
#Query menu 
def query(): 
 print "1.Query student by ID" 
 print "2.Query all students" 
 opt = raw_input("Select:") 
 if opt == "1": 
 queryByID() 
 elif opt == "2": 
 queryAll() 
 #default is query all 
 elif opt == "": 
 queryAll() 
 else : 
 print "Error Input!" 
  
#query ID exist 
def isIDExist(ID): 
 f = open(FILEPATH) 
 flag = 0 
 for eachline in f: 
 temp = eachline.split("\t") 
 if temp[0] == ID: 
  flag+=1 
 f.close() 
 if flag == 0 : 
 return False 
 else: 
 return True 
 
#get information by ID for delete student 
def getInfoByID(stuID): 
 f = open(FILEPATH) 
 i=0 
 for eachLine in f: 
 items = eachLine.split("\t") 
 if items[0] == stuID: 
  i+=1 
  if i==1: 
  print "ID\tNAME\tSEX\tCLASS" 
  print eachLine 
 f.close()  
 
#Query student by ID 
def queryByID(): 
 queryID = raw_input("ID:") 
 f = open(FILEPATH) 
# lines = f.readlines() 
# print lines[0].strip() 
# for line in lines: 
# a = line.split() 
# if queryID == a[1].strip(): 
#  print line.strip() 
 flag = 0 
 for eachline in f: 
 #split by "\t" get as C array temp[4] 
 temp = eachline.split("\t") 
# print temp[0] , temp[1] , temp[2] , temp[3] 
 if temp[0] == queryID: 
  flag+=1 
  if flag == 1: 
  print "ID\tNAME\tSEX\tCLASS" 
  print eachline, 
 if flag == 0 : 
 print "Can't find ID = %s student information" % queryID  
 f.close() 
# f = open(FILEPATH) 
# readLines = f.readlines() 
 
# for eachLine in f: 
# eachLine 
#   
# if eachLine == queryID: 
#  print eachLine 
# f.close() 
 
#Query all students 
def queryAll(): 
 f = open(FILEPATH,"r") 
 i=0 
 for eachLine in f: 
 i+=1 
 if i==1: 
  print "ID\tNAME\tSEX\tCLASS" 
 print eachLine, 
 f.close() 
 if i==0: 
 print "No data!" 
 
def exitProgram(): 
 print "Thank you Bye!" 
# sys.exit() 
 
def init(): 
 if os.path.exists(FILEPATH) : 
 print "Load file successful" 
 else: 
 try: 
  f = open(FILEPATH,"w") 
 except Exception: 
  print "Can't open file" 
  sys.exit() 
 finally: 
  f.close() 
 
if __name__ == '__main__': 
 init() 
 menu()

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

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

Python 相关文章推荐
Python中用于返回绝对值的abs()方法
May 14 Python
python网络爬虫之如何伪装逃过反爬虫程序的方法
Nov 23 Python
用Eclipse写python程序
Feb 10 Python
用TensorFlow实现多类支持向量机的示例代码
Apr 28 Python
详解pyqt5 动画在QThread线程中无法运行问题
May 05 Python
python 列表降维的实例讲解
Jun 28 Python
使用PIL(Python-Imaging)反转图像的颜色方法
Jan 24 Python
Python二进制文件读取并转换为浮点数详解
Jun 25 Python
Python 200行代码实现一个滑动验证码过程详解
Jul 11 Python
解决Django 在ForeignKey中出现 non-nullable field错误的问题
Aug 06 Python
Tensorflow--取tensorf指定列的操作方式
Jun 30 Python
python 实现性别识别
Nov 21 Python
python图书管理系统
Apr 05 #Python
怎么使用pipenv管理你的python项目
Mar 12 #Python
python实现图书管理系统
Mar 12 #Python
python实现快速排序的示例(二分法思想)
Mar 12 #Python
Python中的pack和unpack的使用
Mar 12 #Python
python文本数据相似度的度量
Mar 12 #Python
python使用jieba实现中文分词去停用词方法示例
Mar 11 #Python
You might like
php中echo()和print()、require()和include()等易混淆函数的区别
2012/02/22 PHP
基于PHP遍历数组的方法汇总分析
2013/06/08 PHP
解决CodeIgniter伪静态失效
2014/06/09 PHP
Yii核心组件AssetManager原理分析
2014/12/02 PHP
Laravel框架Auth用户认证操作实例分析
2019/09/29 PHP
Laravel 不同生产环境服务器的判断实践
2019/10/15 PHP
利用Dojo和JSON建立无限级AJAX动态加载的功能模块树
2007/03/24 Javascript
再谈ie和firefox下的document.all属性
2009/10/21 Javascript
浏览器页面区域大小的js获取方法
2013/09/21 Javascript
js数组方法扩展实现数组统计函数
2014/04/09 Javascript
调整小数的格式保留小数点后两位
2014/05/14 Javascript
如何实现移动端浏览器不显示 pc 端的广告
2015/10/15 Javascript
全面解析Bootstrap表单使用方法(表单按钮)
2015/11/24 Javascript
一款简单的jQuery图片标注效果附源码下载
2016/03/22 Javascript
详解AngularJS2 Http服务
2017/06/26 Javascript
JS实现网页抢购功能(触发,终止脚本)
2017/11/27 Javascript
Vue SSR 组件加载问题
2018/05/02 Javascript
使用jquery Ajax实现上传附件功能
2018/10/23 jQuery
[14:57]DOTA2 HEROS教学视频教你分分钟做大人-幽鬼
2014/06/13 DOTA
Python随手笔记之标准类型内建函数
2015/12/02 Python
Python使用redis pool的一种单例实现方式
2016/04/16 Python
使用Python写一个贪吃蛇游戏实例代码
2017/08/21 Python
Python实现的批量修改文件后缀名操作示例
2018/12/07 Python
OpenCV python sklearn随机超参数搜索的实现
2020/01/17 Python
keras load model时出现Missing Layer错误的解决方式
2020/06/11 Python
keras.layer.input()用法说明
2020/06/16 Python
八皇后问题,输出了所有情况,不过有些结果只是旋转了90度
2016/08/15 面试题
环境工程与管理大学毕业生求职信
2013/10/02 职场文书
高级编程求职信模板
2014/02/16 职场文书
国际贸易系求职信
2014/08/09 职场文书
城市规划应届生推荐信
2014/09/08 职场文书
上班迟到检讨书范文300字
2014/11/02 职场文书
创业计划书之蛋糕店
2019/08/29 职场文书
2019年鼓励无偿献血倡议书
2019/09/17 职场文书
美元符号 $
2022/02/17 杂记
浅谈Redis 中的过期删除策略和内存淘汰机制
2022/04/03 Redis