本文实例为大家分享了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()
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python学生管理系统代码实现”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]