(3) 自动输入3名学生的成绩(数学、语文、英语),输出各科目的平均成绩。
时间: 2024-09-20 21:09:27 浏览: 40
3.4 题目7 班级学生平均成绩_平均成绩_
在编程中,特别是使用像Python这样的语言,你可以通过创建一个简单的程序来实现这个功能。假设我们有名为`score`的对象列表,每个对象包含学生姓名和三个科目分数。下面是一个基本的示例:
```python
class Student:
def __init__(self, name):
self.name = name
self.math = 0
self.chinese = 0
self.english = 0
# 输入3名学生的成绩
students = []
for i in range(3):
name = input(f"请输入第{i+1}名学生的姓名:")
math_score = float(input(f"{name}的数学成绩:"))
chinese_score = float(input(f"{name}的语文成绩:"))
english_score = float(input(f"{name}的英语成绩:"))
student = Student(name)
student.math = math_score
student.chinese = chinese_score
student.english = english_score
students.append(student)
# 计算并输出平均成绩
total_scores = [student.math + student.chinese + student.english for student in students]
average_scores = [total / 3 for total in total_scores]
for i, student in enumerate(students):
print(f"{student.name}的平均成绩如下:")
print(f"数学:{average_scores[i]}")
print(f"语文:{average_scores[i]}")
print(f"英语:{average_scores[i]}\n")
阅读全文