2.编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号、语文成绩、英语成绩、数学成绩,教师数据有编号、姓名、职称和部门。班级有10名同学,写代码完成老师录入一个班级的学生成绩,统计输出该班级每位同学的平均成绩,以及该班级各科目的平均成绩。
时间: 2024-06-08 11:07:34 浏览: 104
以下是一个简单的Python程序,可以实现学生和教师数据的输入和显示功能,以及统计输出班级学生的平均成绩和各科目平均成绩:
```python
class Student:
def __init__(self, id, name, class_id, chinese_score, english_score, math_score):
self.id = id
self.name = name
self.class_id = class_id
self.chinese_score = chinese_score
self.english_score = english_score
self.math_score = math_score
class Teacher:
def __init__(self, id, name, title, department):
self.id = id
self.name = name
self.title = title
self.department = department
class Class:
def __init__(self, teacher, students):
self.teacher = teacher
self.students = students
def get_student_avg_score(self):
total_chinese_score = 0
total_english_score = 0
total_math_score = 0
for student in self.students:
total_chinese_score += student.chinese_score
total_english_score += student.english_score
total_math_score += student.math_score
chinese_avg_score = total_chinese_score / len(self.students)
english_avg_score = total_english_score / len(self.students)
math_avg_score = total_math_score / len(self.students)
return (chinese_avg_score, english_avg_score, math_avg_score)
def get_subject_avg_score(self):
total_score = 0
for student in self.students:
total_score += student.chinese_score + student.english_score + student.math_score
total_avg_score = total_score / (len(self.students) * 3)
return total_avg_score
def input_student():
students = []
for i in range(1, 11):
id = input("请输入第{}个学生的编号:".format(i))
name = input("请输入第{}个学生的姓名:".format(i))
class_id = input("请输入第{}个学生的班级编号:".format(i))
chinese_score = float(input("请输入第{}个学生的语文成绩:".format(i)))
english_score = float(input("请输入第{}个学生的英语成绩:".format(i)))
math_score = float(input("请输入第{}个学生的数学成绩:".format(i)))
student = Student(id, name, class_id, chinese_score, english_score, math_score)
students.append(student)
return students
def input_teacher():
id = input("请输入教师的编号:")
name = input("请输入教师的姓名:")
title = input("请输入教师的职称:")
department = input("请输入教师的部门:")
teacher = Teacher(id, name, title, department)
return teacher
def main():
students = input_student()
teacher = input_teacher()
my_class = Class(teacher, students)
chinese_avg_score, english_avg_score, math_avg_score = my_class.get_student_avg_score()
total_avg_score = my_class.get_subject_avg_score()
print("班级各科目平均成绩为:")
print("语文:{:.2f},英语:{:.2f},数学:{:.2f}".format(chinese_avg_score, english_avg_score, math_avg_score))
print("班级学生总体平均成绩为:{:.2f}".format(total_avg_score))
if __name__ == "__main__":
main()
```
你可以按照程序提示输入学生和教师的信息,然后程序会输出班级各科目的平均成绩和班级学生总体平均成绩。
阅读全文