从键盘输入5个学生的信息,包括学号、姓名、数学成绩、计算机成绩。 2)计算每个学生的平均分和总分。 3)按照总分从高到低进行排序。
时间: 2023-12-16 08:28:28 浏览: 60
以下是回答:
```python
# 定义一个学生类
class Student:
def __init__(self, id, name, math_score, computer_score):
self.id = id
self.name = name
self.math_score = math_score
self.computer_score = computer_score
self.total_score = math_score + computer_score
self.average_score = self.total_score / 2
# 重写小于号,用于排序
def __lt__(self, other):
return self.total_score > other.total_score
# 存储学生信息的列表
students = []
# 输入5个学生的信息
for i in range(5):
id = input("请输入第{}个学生的学号:".format(i+1))
name = input("请输入第{}个学生的姓名:".format(i+1))
math_score = float(input("请输入第{}个学生的数学成绩:".format(i+1)))
computer_score = float(input("请输入第{}个学生的计算机成绩:".format(i+1)))
student = Student(id, name, math_score, computer_score)
students.append(student)
# 计算每个学生的平均分和总分
for student in students:
print("学号:{},姓名:{},数学成绩:{},计算机成绩:{},平均分:{},总分:{}".format(student.id, student.name, student.math_score, student.computer_score, student.average_score, student.total_score))
# 按照总分从高到低进行排序
students.sort()
print("按照总分从高到低排序后的结果:")
for student in students:
print("学号:{},姓名:{},数学成绩:{},计算机成绩:{},平均分:{},总分:{}".format(student.id, student.name, student.math_score, student.computer_score, student.average_score, student.total_score))
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)