脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python学生管理系统代码实现

python学生管理系统代码实现

2021-01-21 00:07zhubinqiang Python

这篇文章主要为大家详细介绍了python学生管理系统代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

主函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/zhubinqiang/article/details/7630645

延伸 · 阅读

精彩推荐