班级成绩单,需求如下: (1)学生信息的类 Student, Student 的属性如下: sNO 表示学号; sName 表示姓名;sCourse表示成绩列表;sSum表示所有成绩总分;sAve表示平均分。 (2)班级信息类 Class,Class的属性如下: cName 表示班级名称,cStudent 表示班级学生列表, cCourse 表示班级课程列表 (3)创建班级对象“20物联网班”, 将课程 语文,数学,英语 添加到课程列表 班级有学生信息如下: 202001,张三,语文:88,数学:95,英语:90; 202002,李四,语文:92,数学:98,英语:95。 调用对象方法用表格输出班级成绩单
时间: 2024-02-26 15:59:05 浏览: 64
好的,根据您的需求,我可以提供以下 Python 代码示例:
```python
class Student:
def __init__(self, sNO, sName, sCourse):
self.sNO = sNO
self.sName = sName
self.sCourse = sCourse
self.sSum = sum(sCourse)
self.sAve = self.sSum / len(sCourse)
class Class:
def __init__(self, cName):
self.cName = cName
self.cStudent = []
self.cCourse = []
def add_course(self, course):
self.cCourse.append(course)
def add_student(self, student):
self.cStudent.append(student)
def print_report(self):
print("班级名称:", self.cName)
print("课程列表:", self.cCourse)
print("{:<10}{:<10}{:<10}{:<10}{:<10}".format("学号", "姓名", "语文", "数学", "英语"))
for student in self.cStudent:
print("{:<10}{:<10}{:<10}{:<10}{:<10}".format(student.sNO, student.sName, *student.sCourse))
print("平均分:", [sum(course) / len(course) for course in zip(*[student.sCourse for student in self.cStudent])])
# 创建班级对象
classroom = Class("20物联网班")
# 添加课程
classroom.add_course("语文")
classroom.add_course("数学")
classroom.add_course("英语")
# 添加学生信息
classroom.add_student(Student("202001", "张三", [88, 95, 90]))
classroom.add_student(Student("202002", "李四", [92, 98, 95]))
# 输出班级成绩单
classroom.print_report()
```
运行以上代码,可以得到输出结果:
```
班级名称: 20物联网班
课程列表: ['语文', '数学', '英语']
学号 姓名 语文 数学 英语
202001 张三 88 95 90
202002 李四 92 98 95
平均分: [90.0, 96.5, 92.5]
```
希望能够满足您的需求,如有疑问请随时提出。
阅读全文
相关推荐


















