python学生成绩管理系统创建,供大家参考,具体内容如下
要求编写学生类,班级类,并在电脑运行生成表单,输入一个数字,得到对应的结果。
输出样式
代码如下
学生类
class Student: def __init__(self,sno,name,english,math): self.sno=sno self.name=name self.__english=english self.__math=math @property def english(self): return self.__english @english.setter def english(self,english): self.__english=english if 0<=english<=100 else 0 @property def math(self): return self.__math @math.setter def math(self,math): self.__math=math if 0<=math<=100 else 0 def get_average(self): return (self.english+self.math)/2 def __str__(self): s=str.format('{0:>4d}{1: >4s}{2:>8d}{3:>8d}',self.sno,self.name,self.english,self.math) return s @staticmethod def get_header(): return '{0:>2s}{1: >4s}{2:/u3000>4s}{3: >4s}'.format('学号','姓名','英语','数学') def __eq__(self,other): return self.sno==other.sno def __gt__(self,other): return self.sno>other.sno
测试
#测试if __name__=='__main__': s1=Student(1,'王大海',100, 65) s2=Student(2,'李三',38,45) s3=Student(3,'李四',88,45) print(s1>s2) students=[s2,s1,s3] students.sort() print(students) print(Student.get_header()) for s in students: print(s)
结果
班级类
from student import Studentclass BanJi: def __init__(self,name): self.name=name self.students=[] self.load_data() def load_data(self): with open('data.txt','r')as f: for line in f: d=line.split() self.students.append(Student(int(d[0]),d[1],int(d[2]),int(d[3]))) def show_student(self): print(Student.get_header()) for s in self.students: print(s) def add_student(self,student): if student in self.students: return '此学生已存在' else: self.students.append(student) return'添加成功' def __index_student(self,sno): s=Student(sno,'',0,0) if s not in self.students: return None else: return self.students.index(s) def find_student(self,sno): i = self.__index_student(sno) if i is None: return None else: return self.students[i] def delete_student(self,sno): i = self.__index_student(sno) if i is None: r = '没有学号为{0: d}'.format(sno) else: del self.students[i] r = '删除成功!' return r def update_student(self,sno): s = self.find_student(sno) if s is None: return '没有学号为{0:d}的学生!'.format(sno) else: print(s) print('请输入新的值,直接回车不修改!') name_str = input('姓名:') english_str = input('英语:') math_str = input('数学:') s.name = name_str if len(name_str)>0 else s.name s.name = int(english_str) if len(english_str)>0 else s.english s.name = int(math_str) if len(math_str)>0 else s.math return '修改成功!' def save_data(self): with open('data.txt','w',encoding = 'GBK') as f: for s in self.students: r = str.format('{0:d} {1:s} {2:d} {3:d}/n',s.sno,s.name,s.english,s.math) f.write(r) def sort_by_english(self,reverse = False): self.students.sort(key=lambda x: x.english,reverse = reverse)
新闻热点
疑难解答