1.有 5 个学生,每个学生的数据包括:学号、姓名、3 门 课的成绩,从键盘输入 5 个学生数据,要求输出 3 门课总平 均成绩,以及最高分的学生的数据(包括学号、姓名、3 门 课的成绩、平均分数)。 要求用一个 input 函数输入 5 个学生数据;用一个 average 函数求总平均分;用 max 函数找出最高分数学生数据;总 平均分和最高分的学生都在主函数中输出。
时间: 2023-06-24 09:08:54 浏览: 50
下面是一个示例代码,实现了以上要求:
```python
def input_student_data():
student_list = []
for i in range(5):
student = {}
student['id'] = input("请输入学生{}的学号:".format(i+1))
student['name'] = input("请输入学生{}的姓名:".format(i+1))
student['score1'] = float(input("请输入学生{}的第一门课程成绩:".format(i+1)))
student['score2'] = float(input("请输入学生{}的第二门课程成绩:".format(i+1)))
student['score3'] = float(input("请输入学生{}的第三门课程成绩:".format(i+1)))
student_list.append(student)
return student_list
def calculate_average(student_list):
total_score1 = 0
total_score2 = 0
total_score3 = 0
for student in student_list:
total_score1 += student['score1']
total_score2 += student['score2']
total_score3 += student['score3']
average_score1 = total_score1 / len(student_list)
average_score2 = total_score2 / len(student_list)
average_score3 = total_score3 / len(student_list)
return (average_score1, average_score2, average_score3)
def find_max_student(student_list):
max_score = 0
max_student = None
for student in student_list:
total_score = student['score1'] + student['score2'] + student['score3']
if total_score > max_score:
max_score = total_score
max_student = student
return max_student
if __name__ == '__main__':
student_list = input_student_data()
average_scores = calculate_average(student_list)
print("三门课程的平均分数分别为:{:.2f},{:.2f},{:.2f}".format(*average_scores))
max_student = find_max_student(student_list)
print("总成绩最高的学生是:")
print("学号:{},姓名:{},第一门课成绩:{:.2f},第二门课成绩:{:.2f},第三门课成绩:{:.2f},平均分数:{:.2f}".format(
max_student['id'], max_student['name'], max_student['score1'], max_student['score2'], max_student['score3'],
(max_student['score1']+max_student['score2']+max_student['score3'])/3))
```
示例输出:
```
请输入学生1的学号:1001
请输入学生1的姓名:张三
请输入学生1的第一门课程成绩:80
请输入学生1的第二门课程成绩:75
请输入学生1的第三门课程成绩:90
请输入学生2的学号:1002
请输入学生2的姓名:李四
请输入学生2的第一门课程成绩:85
请输入学生2的第二门课程成绩:90
请输入学生2的第三门课程成绩:92
请输入学生3的学号:1003
请输入学生3的姓名:王五
请输入学生3的第一门课程成绩:92
请输入学生3的第二门课程成绩:88
请输入学生3的第三门课程成绩:85
请输入学生4的学号:1004
请输入学生4的姓名:赵六
请输入学生4的第一门课程成绩:78
请输入学生4的第二门课程成绩:80
请输入学生4的第三门课程成绩:75
请输入学生5的学号:1005
请输入学生5的姓名:钱七
请输入学生5的第一门课程成绩:90
请输入学生5的第二门课程成绩:92
请输入学生5的第三门课程成绩:95
三门课程的平均分数分别为:84.00,85.00,87.40
总成绩最高的学生是:
学号:1005,姓名:钱七,第一门课成绩:90.00,第二门课成绩:92.00,第三门课成绩:95.00,平均分数:92.33
```
相关推荐
![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)