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

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

服务器之家 - 脚本之家 - Python - Python实现学生成绩管理系统

Python实现学生成绩管理系统

2021-01-03 00:52田小思 Python

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

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

基本功能:

输入并存储学生的信息:通过输入学生的学号、姓名、和分数,然后就可以把数据保存在建立的student文件里面。

打印学生的所有信息:通过一个打印函数就可以把所有的信息打印在屏幕上。

修改学生信息:这个功能首先通过查询功能查询出该学生是否存在,如果存在就对该学生的信息进行修改,如果不存在则返回到主界面。

删除学生信息:该功能是对相应的学生进行删除操作,如果学生存在就查找到进行删除。

按学生成绩进行排序: 这个功能是按照学生的成绩进行排序,对学生的信息进行操作。

查找学生信息:这个功能通过输入学号,查找该学生的信息,如果有该学号就输出该学生的信息,没有该学号就提示输入的学号不存在。

初始化功能

系统在开始使用之前先进行初始化功能,判断students.txt文件中是否保存的有学生的信息,如果有就把文件的内容读取出来,供接下来的操作使用,如用没有就初始化一个空的列表,用来保存用户的输入,程序中接下来的所有数据都会保存在该列表中相当与一个数据缓冲区。

首先是打开文件操作,对文件中的内容进行读取操作,由于在文件中保存的内容是由空格进行分割的,并且每一个学生的信息都占用一行,首先读出所有的内容,先进行按照换行进行分割,得到每个人的信息,然后再对每个人的信息进行安装空格分隔,得到每个人的详细信息包括用户的姓名,学号,成绩。

?
1
2
3
4
5
6
7
8
9
10
11
12
def Init(stulist): #初始化函数
 print "初始化......"
 file_object = open('students.txt', 'r')
 for line in file_object:
 stu = Student()
 line = line.strip("\n")
 s = line.split(" ")
 stu.ID = s[0]
 stu.name = s[1]
 stu.score = s[2]
 stulist.append(stu)
print "初始化成功!"

成绩排序实现

这部分代码是按照学生成绩的高低进行排序,在实现的时候,首先是把所有人的成绩放到一个列表里面然后使用插入排序,按照成绩的大小对StuList中保存的学生信息的地址进行排序

?
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
def Sort(stulist): #按学生成绩排序
 Stu = []
 sum_count = []
 for li in stulist:
 temp = []
 temp.append(li.ID)
 temp.append(li.name)
 temp.append(int(li.score1))
 temp.append(int(li.score2))
 temp.append(int(li.score3))
 temp.append(int(li.sum))
 sum_count.append(int(li.sum))
 Stu.append(temp)
 
 #print sum_count
 #print Stu;
 #print stulist
 insertSort(sum_count, stulist)
 #print stulist;
 display(stulist)
 
def insertSort(a, stulist):
 for i in range(len(a)-1):
 #print a,i
 for j in range(i+1,len(a)):
 if a[i]<a[j]:
 temp = stulist[i]
 stulist[i] = stulist[j]
 stulist[j] = temp

界面截图如下:

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
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
# -*- coding: UTF-8 -*-
 
import os
import re
import numpy as np
 
class Student: #定义一个学生类
 def __init__(self):
 self.name = ''
 self.ID =''
 self.score1 = 0
 self.score2 = 0
 self.score1 = 0
 self.sum = 0
 
 
def searchByID(stulist, ID): #按学号查找看是否学号已经存在
 for item in stulist:
 if item.ID == ID:
 return True
 
def Add(stulist,stu): #添加一个学生信息
 if searchByID(stulist, stu.ID) == True:
 print"学号已经存在!"
 return False
 stulist.append(stu)
 print stu.name,stu.ID, stu.score1, stu.score2, stu.score3, stu.sum;
 print "是否要保存学生信息?"
 nChoose = raw_input("Choose Y/N")
 if nChoose == 'Y' or nChoose == 'y':
 file_object = open("students.txt", "a")
 file_object.write(stu.ID)
 file_object.write(" ")
 file_object.write(stu.name)
 file_object.write(" ")
 file_object.write(str(stu.score1))
 file_object.write(" ")
 file_object.write(str(stu.score2))
 file_object.write(" ")
 file_object.write(str(stu.score3))
 file_object.write(" ")
 file_object.write(str(stu.sum))
 file_object.write("\n")
 file_object.close()
 print u"保存成功!"
 
def Search(stulist, ID): #搜索一个学生信息
 print u"学号 姓名 语文 数学 英语 总分"
 count = 0
 for item in stulist:
 if item.ID == ID:
 print item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum
 break
 count = 0
 if count == len(stulist):
 print "没有该学生学号!"
 
def Del(stulist, ID): #删除一个学生信息
 count = 0
 for item in stulist:
 if item.ID == ID:
 stulist.remove(item)
 print "删除成功!"
 break
 count +=1
 # if count == len(stulist):
 # print "没有该学生学号!"
 file_object = open("students.txt", "w")
 for stu in stulist:
 print stu.ID, stu.name, stu.score1,stu.score2, stu.score3, stu.sum
 file_object.write(stu.ID)
 file_object.write(" ")
 file_object.write(stu.name)
 file_object.write(" ")
 file_object.write(str(stu.score1))
 file_object.write(" ")
 file_object.write(str(stu.score2))
 file_object.write(" ")
 file_object.write(str(stu.score3))
 file_object.write(" ")
 file_object.write(str(stu.sum))
 file_object.write("\n")
 file_object.close()
 # print "保存成功!"
 file_object.close()
def Change(stulist, ID):
 count = 0
 for item in stulist:
 if item.ID == ID:
 stulist.remove(item)
 file_object = open("students.txt", "w")
 for stu in stulist:
 #print li.ID, li.name, li.score
 file_object.write(stu.ID)
 file_object.write(" ")
 file_object.write(stu.name)
 file_object.write(" ")
 file_object.write(str(stu.score1))
 file_object.write(" ")
 file_object.write(str(stu.score2))
 file_object.write(" ")
 file_object.write(str(stu.score3))
 file_object.write(" ")
 file_object.write(str(stu.sum))
 file_object.write("\n")
 # print "保存成功!"
 file_object.close()
 stu = Student()
 stu.name = raw_input("请输入学生的姓名")
 while True:
 stu.ID = raw_input("请输入学生的ID")
 p = re.compile('^[0-9]{3}$')
 if p.match(stu.ID):
 break
 else:
 print "输入的有错误!"
 while True:
 stu.score1 = int(raw_input("请输入学生语文成绩"))
 if stu.score1 <= 100 and stu.score1 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score2 = int(raw_input("请输入学生数学成绩"))
 if stu.score2 <= 100 and stu.score2 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score3 = int(raw_input("请输入学生英语成绩"))
 if stu.score3 <= 100 and stu.score3 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 stu.sum = stu.score1 + stu.score2 + stu.score3
 Add(stulist,stu)
def display(stulist): #显示所有学生信息
 print u"学号 姓名 语文 数学 英语 总分"
 for item in stulist:
 print item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum
 
def Sort(stulist): #按学生成绩排序
 Stu = []
 sum_count = []
 for li in stulist:
 temp = []
 temp.append(li.ID)
 temp.append(li.name)
 temp.append(int(li.score1))
 temp.append(int(li.score2))
 temp.append(int(li.score3))
 temp.append(int(li.sum))
 sum_count.append(int(li.sum))
 Stu.append(temp)
 
 #print sum_count
 #print Stu;
 #print stulist
 insertSort(sum_count, stulist)
 #print stulist;
 display(stulist)
 
def insertSort(a, stulist):
 for i in range(len(a)-1):
 #print a,i
 for j in range(i+1,len(a)):
 if a[i]<a[j]:
 temp = stulist[i]
 stulist[i] = stulist[j]
 stulist[j] = temp
 #return a
 
def Init(stulist): #初始化函数
 print "初始化......"
 file_object = open('students.txt', 'r')
 for line in file_object:
 stu = Student()
 line = line.strip("\n")
 s = line.split(" ")
 stu.ID = s[0]
 stu.name = s[1]
 stu.score1 = s[2]
 stu.score2 = s[3]
 stu.score3 = s[4]
 stu.sum = s[5]
 stulist.append(stu)
 file_object.close()
 print "初始化成功!"
 main()
 
def main(): #主函数 该程序的入口函数
 while True:
 print "*********************"
 print u"--------菜单---------"
 print u"增加学生信息--------1"
 print u"查找学生信息--------2"
 print u"删除学生信息--------3"
 print u"修改学生信息--------4"
 print u"所有学生信息--------5"
 print u"按照分数排序--------6"
 print u"退出程序------------0"
 print "*********************"
 
 nChoose = raw_input("请输入你的选择:")
 if nChoose == "1":
 stu = Student()
 stu.name = raw_input("请输入学生的姓名")
 while True:
 stu.ID = raw_input("请输入学生的ID")
 p = re.compile('^[0-9]{3}$')
 if p.match(stu.ID):
 break
 else:
 print "输入的有错误!"
 while True:
 stu.score1 = int(raw_input("请输入学生语文成绩"))
 if stu.score1 <= 100 and stu.score1 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score2 = int(raw_input("请输入学生数学成绩"))
 if stu.score2 <= 100 and stu.score2 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 while True:
 stu.score3 = int(raw_input("请输入学生英语成绩"))
 if stu.score3 <= 100 and stu.score3 > 0 :
 break
 else:
 print "输入的学生成绩有错误!"
 stu.sum = stu.score1 + stu.score2 + stu.score3
 Add(stulist,stu)
 
 if nChoose == '2':
 ID = raw_input("请输入学生的ID")
 Search(stulist, ID)
 
 if nChoose == '3':
 ID = raw_input("请输入学生的ID")
 Del(stulist, ID)
 if nChoose == '4':
 ID = raw_input("请输入学生的ID")
 Change(stulist, ID)
 
 if nChoose == '5':
 display(stulist)
 
 if nChoose == '6':
 Sort(stulist)
 
 
 if nChoose == '0':
 break
 
if __name__ == '__main__':
 stulist =[]
Init(stulist)

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

原文链接:http://blog.csdn.net/tian_123456789/article/details/78914692

延伸 · 阅读

精彩推荐