python学生成绩管理系统创建

python学生成绩管理系统创建

要求编写学生类,班级类,并在电脑运行生成表单,输入一个数字,得到对应的结果。

输出样式
python学生成绩管理系统创建_第1张图片

代码如下

学生类

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) 

结果

python学生成绩管理系统创建_第2张图片

班级类

from student import Student

class 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)

学生管理系统

import os
from student import Student
from banji import BanJi

menu = """

*********************

    学生成绩管理系统
    1.浏览学生成绩
    2.按学号查找学生
    3.按英语成绩排序
    4.添加学生
    5.删除学生
    6.修改学生信息
    9.保存
    0.退出
    
*********************

"""
tip = '输入(0-9)选择操作:'

choice = ''

b = BanJi('1802')

os.system('cls')
print(menu)

while True:
    choice = input(tip)
    os.system('cls')
    print(menu)
    if choice == '0':
        print('数据以保存!')
        break
    elif choice == '1':
        b.show_student()
    elif choice == '2':
        sno = int(input('请输入您需要查询的学生的学号:'))
        s = b.find_student(sno)
        if s is None:
            print('无此学号的学生!')
        else:
            print(s)
    elif choice == '3':
        b.sort_by_english()
        b.show_student()
    elif choice == '4':
        print('请输入学生信息:')
        sno = int(input('学号:'))
        name = input('姓名:')
        english = int(input('英语:'))
        math = int(input('数学:'))
        student = Student(sno,name,english,math)
        r = b.add_student(student)
        print(r)
    elif choice == '5':
        sno = int(input('请输入要删除的学生的学号:'))
        r = b.delete_student(sno)
        print(r)
        
    elif choice == '6':
        sno = int(input('请输入要修改的学生的学号:'))
        r = b.update_student(sno)
        print(r)
        
    elif choice == '9':
        b.save_data()
        print('保存成功!')
    else:
        print('输入错误!')

用spyder或者cmd运行都可以。

你可能感兴趣的:(python)