Table of contents
The specific implementation process:
Define the student management class
Define the display student information function
Seeking the highest score in the course
Find the minimum score for the course
Finally define the menu function and the main function:
functional module:
Basic information management and student achievement management.
The main functions of the basic information management module include adding, deleting, modifying, displaying student information and importing and exporting student data,
The main function of the student performance management module is to count the highest score, the lowest score and the average score of the course.
Implementation ideas:
- Design a student class, including data members such as student number, name, Chinese score, math score and English score.
- Introduce OS module for importing and exporting path file information
- Design a student management class to define the specific functional functions of each module.
- Design a main menu and two submenus to manage basic student information and student performance information respectively
Run a functional demo:
After the student information management system is started, it first enters the main interface of the system, and waits for the user to input commands to select the corresponding functions.
If the user enters the "info" command, it will enter the student basic information management sub-function module.
In the student basic information management interface, the user can add, delete, modify and display the basic student information by inputting corresponding commands.
Student Basic Information Menu
Add student information
delete student information
Modify student information
display student information
If the user enters the "score" command, it will enter the student achievement management sub-function module.
In the student achievement management interface, the user can select the corresponding function for achievement processing.
The average score:
Maximum
Lowest score
The specific implementation process:
Define the student class:
class Student:
def __init__(self,no,name,chinese,math,english):
self.no = no
self.name = name
self.chinese = int(chinese)
self.math = int(math)
self.english = int(english)
Define the student management class
class StudentList:
def __init__(self):
self.stulist = []
Define the display student information function
Define the display student information function under the student management class:
def show(self):
#显示学生信息
print('{:8}\t{:8}\t{:8}\t{:8}\t{:8}'
.format('学号','姓名','语文','数学','英语'))
for stu in self.stulist:
print('{:8}\t{:8}\t{:<8}\t{:<8}\t{:<8}'
.format(stu.no,stu.name,stu.chinese,stu.math,stu.english))
Enter the grade function:
def __enterScore(self,message):
#成绩输入
while True:
try:
score = input(message)
if 0 <= int(score) <= 100:
break
else:
print("输入错误,成绩应在0到100之间")
except:
print("输入错误,成绩应在0到100之间")
return score
def __exists(self,no):
#判断学号是否存在
for stu in self.stulist:
if stu.no == no:
return True
else:
return False
Add student information:
def insert(self):
#添加学生信息
while True:
no = input('学号:')
if self.__exists(no):
print('该学号已存在')
else:
name = input('姓名:')
chinese = self.__enterScore('语文成绩:')
math = self.__enterScore('数学成绩:')
english = self.__enterScore('英语成绩:')
stu = Student(no,name,chinese,math,english)
self.stulist.append(stu)
choice = input('继续添加(y/n)?').lower()
if choice == 'n':
break
delete student information
def delete(self):
#删除学生信息
while True:
no = input('请输入要删除的学生学号:')
for stu in self.stulist[::]:
if stu.no == no:
self.stulist.remove(stu)
print('删除成功')
break
else:
print('该学号不存在')
choice = input('继续删除(y/n)?').lower()
if choice == 'n':
break
Modify student information
def update(self):
#修改学生信息
while True:
no = input('请输入要修改的学生学号:')
if self.__exists(no):
for stu in self.stulist:
if stu.no == no:
stu.name = input('姓名:')
stu.chinese = int(self.__enterScore('语文成绩:'))
stu.math = int(self.__enterScore('数学成绩:'))
stu.english = int(self.__enterScore('英语成绩:'))
print('修改成功')
break
else:
print('该学号不存在')
choice = input('继续修改(y/n)?').lower()
if choice == 'n':
break
Import student information
def load(self,fn):
#导入学生信息
if os.path.exists(fn):
try:
with open(fn,'r',encoding = 'utf-8') as fp:
while True:
fs = fp.readline().strip('\n')
if not fs:
break
else:
stu = Student(*fs.split(','))
if self.__exists(stu.no):
print('该学号已存在')
else:
self.stulist.append(stu)
print('导入完毕')
except:
print('error...') #要导入的文件不是utf-8编码,或是字段数不匹配等
else:
print('要导入的文件不存在')
Export student information
def save(self,fn):
#导出学生信息
with open(fn,'w',encoding = 'utf-8') as fp:
for stu in self.stulist:
fp.write(stu.no + ',')
fp.write(stu.name + ',')
fp.write(str(stu.chinese) + ',')
fp.write(str(stu.math) + ',')
fp.write(str(stu.english) + '\n')
print("导出完毕")
Find course average
def scoreavg(self):
#求课程平均分
length = len(self.stulist)
if length > 0:
chinese_avg = sum([stu.chinese for stu in self.stulist])/length
math_avg = sum([stu.math for stu in self.stulist])/length
english_avg = sum([stu.english for stu in self.stulist])/length
print('语文成绩平均分是:%.2f'%chinese_avg)
print('数学成绩平均分是:%.2f'%math_avg)
print('英语成绩平均分是:%.2f'%english_avg)
else:
print('尚没有学生成绩...')
Seeking the highest score in the course
def scoremax(self):
#求课程最高分
if len(self.stulist) > 0:
chinese_max = max([stu.chinese for stu in self.stulist])
math_max = max([stu.math for stu in self.stulist])
english_max = max([stu.english for stu in self.stulist])
print('语文成绩最高分是:%d'%chinese_max)
print('数学成绩最高分是:%d'%math_max)
print('英语成绩最高分是:%d'%english_max)
else:
print('尚没有学生成绩...')
Find the minimum score for the course
def scoremin(self):
#求课程最低分
if len(self.stulist) > 0:
chinese_min = min([stu.chinese for stu in self.stulist])
math_min = min([stu.math for stu in self.stulist])
english_min = min([stu.english for stu in self.stulist])
print('语文成绩最低分是:%d'%chinese_min)
print('数学成绩最低分是:%d'%math_min)
print('英语成绩最低分是:%d'%english_min)
else:
print('尚没有学生成绩...')
Finally define the menu function and the main function:
def infoprocess(self):
#基本信息管理
print('学生基本信息管理'.center(20,'-'))
print('load----------导入学生信息')
print('insert--------添加学生信息')
print('delete--------删除学生信息')
print('update--------修改学生信息')
print('show----------显示学生信息')
print('save----------导出学生信息')
print('return--------返回')
print('-'*28)
while True:
s = input('info>').strip().lower()
if s == 'load':
fn = input('请输入要导入的文件名:')
self.load(fn)
elif s == 'show':
self.show()
elif s == 'insert':
self.insert()
elif s == 'delete':
self.delete()
elif s == 'update':
self.update()
elif s == 'save':
fn = input('请输入要导出的文件名:')
self.save(fn)
elif s =='return':
break
else:
print('输入错误')
def scoreprocess(self):
#学生成绩统计
print('学生成绩统计'.center(24,'='))
print('avg --------课程平均分')
print('max --------课程最高分')
print('min --------课程最低分')
print('return --------返回')
print(''.center(30,'='))
while True:
s = input('score>').strip().lower()
if s == 'avg':
self.scoreavg()
elif s == 'max':
self.scoremax()
elif s == 'min':
self.scoremin()
elif s == 'return':
break
else:
print('输入错误')
def main(self):
#主控函数
while True:
print('学生信息管理系统V1.0'.center(24,'='))
print('info -------学生基本信息管理')
print('score -------学生成绩统计')
print('exit -------退出系统')
print(''.center(32,'='))
s = input('main>').strip().lower()
if s == 'info':
self.infoprocess()
elif s == 'score':
self.scoreprocess()
elif s == 'exit':
break
else:
print('输入错误')
if __name__ == '__main__':
st = StudentList()
st.main()
Full code:
import os
class Student:
def __init__(self,no,name,chinese,math,english):
self.no = no
self.name = name
self.chinese = int(chinese)
self.math = int(math)
self.english = int(english)
class StudentList:
def __init__(self):
self.stulist = []
def show(self):
#显示学生信息
print('{:8}\t{:8}\t{:8}\t{:8}\t{:8}'
.format('学号','姓名','语文','数学','英语'))
for stu in self.stulist:
print('{:8}\t{:8}\t{:<8}\t{:<8}\t{:<8}'
.format(stu.no,stu.name,stu.chinese,stu.math,stu.english))
def __enterScore(self,message):
#成绩输入
while True:
try:
score = input(message)
if 0 <= int(score) <= 100:
break
else:
print("输入错误,成绩应在0到100之间")
except:
print("输入错误,成绩应在0到100之间")
return score
def __exists(self,no):
#判断学号是否存在
for stu in self.stulist:
if stu.no == no:
return True
else:
return False
def insert(self):
#添加学生信息
while True:
no = input('学号:')
if self.__exists(no):
print('该学号已存在')
else:
name = input('姓名:')
chinese = self.__enterScore('语文成绩:')
math = self.__enterScore('数学成绩:')
english = self.__enterScore('英语成绩:')
stu = Student(no,name,chinese,math,english)
self.stulist.append(stu)
choice = input('继续添加(y/n)?').lower()
if choice == 'n':
break
def delete(self):
#删除学生信息
while True:
no = input('请输入要删除的学生学号:')
for stu in self.stulist[::]:
if stu.no == no:
self.stulist.remove(stu)
print('删除成功')
break
else:
print('该学号不存在')
choice = input('继续删除(y/n)?').lower()
if choice == 'n':
break
def update(self):
#修改学生信息
while True:
no = input('请输入要修改的学生学号:')
if self.__exists(no):
for stu in self.stulist:
if stu.no == no:
stu.name = input('姓名:')
stu.chinese = int(self.__enterScore('语文成绩:'))
stu.math = int(self.__enterScore('数学成绩:'))
stu.english = int(self.__enterScore('英语成绩:'))
print('修改成功')
break
else:
print('该学号不存在')
choice = input('继续修改(y/n)?').lower()
if choice == 'n':
break
def load(self,fn):
#导入学生信息
if os.path.exists(fn):
try:
with open(fn,'r',encoding = 'utf-8') as fp:
while True:
fs = fp.readline().strip('\n')
if not fs:
break
else:
stu = Student(*fs.split(','))
if self.__exists(stu.no):
print('该学号已存在')
else:
self.stulist.append(stu)
print('导入完毕')
except:
print('error...') #要导入的文件不是utf-8编码,或是字段数不匹配等
else:
print('要导入的文件不存在')
def save(self,fn):
#导出学生信息
with open(fn,'w',encoding = 'utf-8') as fp:
for stu in self.stulist:
fp.write(stu.no + ',')
fp.write(stu.name + ',')
fp.write(str(stu.chinese) + ',')
fp.write(str(stu.math) + ',')
fp.write(str(stu.english) + '\n')
print("导出完毕")
def scoreavg(self):
#求课程平均分
length = len(self.stulist)
if length > 0:
chinese_avg = sum([stu.chinese for stu in self.stulist])/length
math_avg = sum([stu.math for stu in self.stulist])/length
english_avg = sum([stu.english for stu in self.stulist])/length
print('语文成绩平均分是:%.2f'%chinese_avg)
print('数学成绩平均分是:%.2f'%math_avg)
print('英语成绩平均分是:%.2f'%english_avg)
else:
print('尚没有学生成绩...')
def scoremax(self):
#求课程最高分
if len(self.stulist) > 0:
chinese_max = max([stu.chinese for stu in self.stulist])
math_max = max([stu.math for stu in self.stulist])
english_max = max([stu.english for stu in self.stulist])
print('语文成绩最高分是:%d'%chinese_max)
print('数学成绩最高分是:%d'%math_max)
print('英语成绩最高分是:%d'%english_max)
else:
print('尚没有学生成绩...')
def scoremin(self):
#求课程最低分
if len(self.stulist) > 0:
chinese_min = min([stu.chinese for stu in self.stulist])
math_min = min([stu.math for stu in self.stulist])
english_min = min([stu.english for stu in self.stulist])
print('语文成绩最低分是:%d'%chinese_min)
print('数学成绩最低分是:%d'%math_min)
print('英语成绩最低分是:%d'%english_min)
else:
print('尚没有学生成绩...')
def infoprocess(self):
#基本信息管理
print('学生基本信息管理'.center(20,'-'))
print('load----------导入学生信息')
print('insert--------添加学生信息')
print('delete--------删除学生信息')
print('update--------修改学生信息')
print('show----------显示学生信息')
print('save----------导出学生信息')
print('return--------返回')
print('-'*28)
while True:
s = input('info>').strip().lower()
if s == 'load':
fn = input('请输入要导入的文件名:')
self.load(fn)
elif s == 'show':
self.show()
elif s == 'insert':
self.insert()
elif s == 'delete':
self.delete()
elif s == 'update':
self.update()
elif s == 'save':
fn = input('请输入要导出的文件名:')
self.save(fn)
elif s =='return':
break
else:
print('输入错误')
def scoreprocess(self):
#学生成绩统计
print('学生成绩统计'.center(24,'='))
print('avg --------课程平均分')
print('max --------课程最高分')
print('min --------课程最低分')
print('return --------返回')
print(''.center(30,'='))
while True:
s = input('score>').strip().lower()
if s == 'avg':
self.scoreavg()
elif s == 'max':
self.scoremax()
elif s == 'min':
self.scoremin()
elif s == 'return':
break
else:
print('输入错误')
def main(self):
#主控函数
while True:
print('学生信息管理系统V1.0'.center(24,'='))
print('info -------学生基本信息管理')
print('score -------学生成绩统计')
print('exit -------退出系统')
print(''.center(32,'='))
s = input('main>').strip().lower()
if s == 'info':
self.infoprocess()
elif s == 'score':
self.scoreprocess()
elif s == 'exit':
break
else:
print('输入错误')
if __name__ == '__main__':
st = StudentList()
st.main()