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通过pil将图片转换成黑白效果的方法
Mar 16 Python
如何在Python中编写并发程序
Feb 27 Python
Python文本特征抽取与向量化算法学习
Dec 22 Python
对Python中type打开文件的方式介绍
Apr 28 Python
python list是否包含另一个list所有元素的实例
May 04 Python
python3+selenium实现126邮箱登陆并发送邮件功能
Jan 23 Python
详解用python生成随机数的几种方法
Aug 04 Python
用python3 urllib破解有道翻译反爬虫机制详解
Aug 14 Python
基于Django signals 信号作用及用法详解
Mar 28 Python
使用python采集Excel表中某一格数据
May 14 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
May 22 Python
Python如何输出百分比
Jul 31 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单例模式实现(对象只被创建一次)
2012/12/05 PHP
twig里使用js变量的方法
2016/02/05 PHP
jquery实现倒计时代码分享
2014/06/13 Javascript
JQuery+CSS实现图片上放置按钮的方法
2015/05/29 Javascript
java必学必会之static关键字
2015/12/03 Javascript
jquery获取select选中值的方法分析
2015/12/22 Javascript
JS遍历ul下的li点击弹出li的索引的实现方法
2016/09/19 Javascript
微信小程序之数据双向绑定与数据操作
2017/05/12 Javascript
详解Angular 自定义结构指令
2017/06/21 Javascript
.vue文件 加scoped 样式不起作用的解决方法
2018/05/28 Javascript
利用React Router4实现的服务端直出渲染(SSR)
2019/01/07 Javascript
JS实现商城秒杀倒计时功能(动态设置秒杀时间)
2019/12/12 Javascript
在weex中愉快的使用scss的方法步骤
2020/01/02 Javascript
JavaScript简单编程实例学习
2020/02/14 Javascript
vue动态渲染svg、添加点击事件的实现
2020/03/13 Javascript
Vue实现图片轮播组件思路及实例解析
2020/05/11 Javascript
Python的Tornado框架实现图片上传及图片大小修改功能
2016/06/30 Python
Python操作Redis之设置key的过期时间实例代码
2018/01/25 Python
对Tensorflow中的变量初始化函数详解
2018/07/27 Python
详解python如何在django中为用户模型添加自定义权限
2018/10/15 Python
Python实现的银行系统模拟程序完整案例
2019/04/12 Python
详解程序意外中断自动重启shell脚本(以Python为例)
2019/07/26 Python
python中append实例用法总结
2019/07/30 Python
python3获取url文件大小示例代码
2019/09/18 Python
解决python 上传图片限制格式问题
2019/10/30 Python
Python 脚本拉取 Docker 镜像问题
2019/11/10 Python
python数据库编程 ODBC方式实现通讯录
2020/03/27 Python
Holiday Inn中国官网:IHG旗下假日酒店预订
2018/04/08 全球购物
美国现代家具购物网站:LexMod
2019/01/09 全球购物
学生实习介绍信
2014/01/15 职场文书
二手房购房协议书范本
2014/10/05 职场文书
师德师风自查材料
2014/10/14 职场文书
党员批评与自我批评总结
2014/10/15 职场文书
技术员岗位职责
2015/02/04 职场文书
事业单位财务人员岗位职责
2015/04/14 职场文书
银行文明优质服务培训心得体会
2016/01/09 职场文书