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 isinstance判断对象类型
Sep 06 Python
python ip正则式
May 07 Python
Python使用struct处理二进制的实例详解
Sep 11 Python
python编写分类决策树的代码
Dec 21 Python
给我一面国旗 python帮你实现
Sep 30 Python
python实现大学人员管理系统
Oct 25 Python
PyTorch中permute的用法详解
Dec 30 Python
pytorch实现mnist分类的示例讲解
Jan 10 Python
Python使用PyQt5/PySide2编写一个极简的音乐播放器功能
Feb 07 Python
利用Python发送邮件或发带附件的邮件
Nov 12 Python
PyQt QMainWindow的使用示例
Mar 24 Python
python 学习GCN图卷积神经网络
May 11 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
咖啡历史、消费和行业趋势
2021/03/03 咖啡文化
PHP PDO函数库详解
2010/04/27 PHP
PHP JSON格式数据交互实例代码详解
2011/01/13 PHP
php代码运行时间查看类代码分享
2011/08/06 PHP
ThinkPHP写第一个模块应用
2012/02/20 PHP
php比较两个绝对时间的大小
2014/01/31 PHP
PHP操作Redis数据库常用方法示例
2018/08/25 PHP
Laravel jwt 多表(多用户端)验证隔离的实现
2019/12/18 PHP
jQuery 使用手册(二)
2009/09/23 Javascript
关于javascript中的typeof和instanceof介绍
2012/12/04 Javascript
打开新窗口关闭当前页面不弹出关闭提示js代码
2013/03/18 Javascript
Javascript实现页面跳转的几种方式分享
2013/10/26 Javascript
jQuery 设置 CSS 属性示例介绍
2014/01/16 Javascript
动态添加删除表格行的js实现代码
2014/02/28 Javascript
jQuery实现单击和鼠标感应事件
2015/02/01 Javascript
js实现文字在按钮上滚动的方法
2015/08/20 Javascript
基于jQuery实现仿百度首页换肤背景图片切换代码
2015/08/25 Javascript
JS+CSS实现类似QQ好友及黑名单效果的树型菜单
2015/09/22 Javascript
JavaScript判断用户名和密码不能为空的实现代码
2016/05/16 Javascript
微信小程序 生命周期和页面的生命周期详细介绍
2017/01/19 Javascript
canvas实现环形进度条效果
2017/03/23 Javascript
基于JavaScript实现移动端无限加载分页
2017/03/27 Javascript
微信小程序 仿美团分类菜单 swiper分类菜单
2017/04/12 Javascript
python中cPickle用法例子分享
2014/01/03 Python
python魔法方法-属性转换和类的表示详解
2016/07/22 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
2018/12/10 Python
一篇文章搞懂Python的类与对象名称空间
2018/12/10 Python
django框架model orM使用字典作为参数,保存数据的方法分析
2019/06/24 Python
python简单鼠标自动点击某区域的实例
2019/06/25 Python
详解HTML5 window.postMessage与跨域
2017/05/11 HTML / CSS
Vans澳大利亚官网:购买鞋子、服装及配件
2019/09/05 全球购物
金蝶的一道SQL笔试题
2012/12/18 面试题
晚会邀请函范文
2014/01/24 职场文书
校园学雷锋广播稿
2014/10/08 职场文书
幼儿园教师教育随笔
2015/08/14 职场文书
python实现批量提取指定文件夹下同类型文件
2021/04/05 Python