有12个学生,每个学生的数据包括学号、姓名、三门课程的成绩及其平均分。首先输入这12个学生的数据,然后再计算每个学生3门课程的平均分,并输出第6号学生的所有信息。python
时间: 2023-12-15 20:53:12 浏览: 32
可以使用列表和字典来存储学生的数据。以下是一个示例代码:
```python
students = []
for i in range(12):
student = {}
student['id'] = input("请输入第{}个学生的学号:".format(i+1))
student['name'] = input("请输入第{}个学生的姓名:".format(i+1))
scores = []
for j in range(3):
score = float(input("请输入第{}个学生的第{}门课程成绩:".format(i+1, j+1)))
scores.append(score)
student['scores'] = scores
student['average'] = sum(scores) / 3
students.append(student)
print("第6号学生的信息如下:")
print(students[5])
```
这个代码首先创建了一个空列表 `students`,然后使用循环输入每个学生的数据,包括学号、姓名、三门课程的成绩和平均分。最后输出第6号学生的所有信息。
注意,这个代码忽略了输入数据的合法性检查,如果需要可以自行添加。
相关问题
有十个学生,每个学生的数据包括学号姓名三门课的成绩。读入这十个学生的数据,要求输出三门课程的总平均成绩,以及个人平均分最高的学生的数据
好的,针对您的问题,以下是代码实现:
```python
# 读入十个学生的数据
students = []
for i in range(10):
student = {}
student['学号'] = input('请输入学号:')
student['姓名'] = input('请输入姓名:')
student['成绩1'] = float(input('请输入第一门课程成绩:'))
student['成绩2'] = float(input('请输入第二门课程成绩:'))
student['成绩3'] = float(input('请输入第三门课程成绩:'))
students.append(student)
# 计算三门课程的总平均成绩
average_scores = {'成绩1': 0, '成绩2': 0, '成绩3': 0}
for student in students:
average_scores['成绩1'] += student['成绩1']
average_scores['成绩2'] += student['成绩2']
average_scores['成绩3'] += student['成绩3']
average_scores['成绩1'] /= 10
average_scores['成绩2'] /= 10
average_scores['成绩3'] /= 10
print('三门课程的总平均成绩分别为:成绩1:{:.2f},成绩2:{:.2f},成绩3:{:.2f}'.format(average_scores['成绩1'], average_scores['成绩2'], average_scores['成绩3']))
# 找出个人平均分最高的学生的数据
max_average_score = 0
max_average_score_student = {}
for student in students:
average_score = (student['成绩1'] + student['成绩2'] + student['成绩3']) / 3
if average_score > max_average_score:
max_average_score = average_score
max_average_score_student = student
print('个人平均分最高的学生的数据为:学号:{},姓名:{},成绩1:{:.2f},成绩2:{:.2f},成绩3:{:.2f}'.format(max_average_score_student['学号'], max_average_score_student['姓名'], max_average_score_student['成绩1'], max_average_score_student['成绩2'], max_average_score_student['成绩3']))
```
注意,这里使用了一个字典来存储每个学生的数据,其中键为学号、姓名、成绩1、成绩2、成绩3。同时,为了方便计算平均分,我们使用了一个字典来存储三门课程的总成绩,最后再除以10得到平均分。而对于找出个人平均分最高的学生的数据,则需要遍历所有学生,计算每个学生的平均分,并与当前最高平均分进行比较,找出最高分对应的学生即可。
有20个学生,每个学生的数据包括学号、姓名、三门课程的成绩及其平均分。首先输入这20个学生的数据,然后再计算每个学生3门课程的平均分,并输出第12号学生的所有信息。
以下是一个示例代码,可以实现上述功能:
```python
class Student:
def __init__(self, id, name, score1, score2, score3):
self.id = id
self.name = name
self.score1 = score1
self.score2 = score2
self.score3 = score3
def get_average_score(self):
return (self.score1 + self.score2 + self.score3) / 3
students = []
# 输入学生数据
for i in range(20):
id = input("请输入第%d个学生的学号:" % (i+1))
name = input("请输入第%d个学生的姓名:" % (i+1))
score1 = float(input("请输入第%d个学生的第1门课程成绩:" % (i+1)))
score2 = float(input("请输入第%d个学生的第2门课程成绩:" % (i+1)))
score3 = float(input("请输入第%d个学生的第3门课程成绩:" % (i+1)))
student = Student(id, name, score1, score2, score3)
students.append(student)
# 计算每个学生的平均分
for student in students:
average_score = student.get_average_score()
print("%s的平均分为%.2f" % (student.name, average_score))
# 输出第12号学生的所有信息
student = students[11]
print("第12号学生的学号:%s" % student.id)
print("第12号学生的姓名:%s" % student.name)
print("第12号学生的第1门课程成绩:%.2f" % student.score1)
print("第12号学生的第2门课程成绩:%.2f" % student.score2)
print("第12号学生的第3门课程成绩:%.2f" % student.score3)
print("第12号学生的平均分为:%.2f" % student.get_average_score())
```
这段代码定义了一个 `Student` 类,包含学生的学号、姓名、三门课程的成绩,以及计算平均分的方法。首先通过输入来初始化20个学生的数据,并将它们存储在一个列表中。然后遍历列表,计算每个学生的平均分并输出。最后输出第12号学生的所有信息。
相关推荐
![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)