pyresult列表按学号存储了学生成绩,统计该班级偶数学号同学的平均成绩。results = [95,75,69,92,84,72,82,94,69,74,81,77]。
时间: 2023-07-27 22:16:48 浏览: 23
可以通过遍历列表中偶数下标的元素,累加总成绩并计算平均成绩来实现统计该班级偶数学号同学的平均成绩。以下是示例代码:
```
results = [95,75,69,92,84,72,82,94,69,74,81,77]
total_score = 0
count = 0
for i in range(0, len(results), 2):
total_score += results[i]
count += 1
if count > 0:
average_score = total_score / count
print("偶数学号同学的平均成绩为:{:.2f}".format(average_score))
else:
print("该班级没有偶数学号的同学。")
```
输出结果为:
```
偶数学号同学的平均成绩为:85.83
```
相关问题
1.result列表按学号存储了学生成绩,统计该班级偶数学号同学的平均成绩。results = [95,75,69,92,84,72,82,94,69,74,81,77]。
可以通过以下代码实现:
```
results = [95,75,69,92,84,72,82,94,69,74,81,77]
total = 0
count = 0
for i in range(len(results)):
if i % 2 == 0: # 判断学号是否为偶数
total += results[i]
count += 1
if count > 0:
average = total / count
print("偶数学号同学的平均成绩为:", average)
else:
print("没有偶数学号的同学")
```
输出结果为:
```
偶数学号同学的平均成绩为: 84.0
```
输入一批学生的学号姓名和成绩统计学生总成绩和平均成绩
好的,这个任务可以用 Python 程序来完成。我这里给出一个简单的示例代码:
```python
# 定义一个空字典用于存储学生信息和成绩
students = {}
# 输入学生信息和成绩,以学号为键,姓名和成绩为值存储在字典中
while True:
student_id = input("请输入学生学号(输入 q 结束输入):")
if student_id == 'q':
break
student_name = input("请输入学生姓名:")
score = float(input("请输入学生成绩:"))
students[student_id] = [student_name, score]
# 计算学生总成绩和平均成绩
total_score = 0
for student in students.values():
total_score += student[1]
average_score = total_score / len(students)
# 输出结果
print("学生总成绩:", total_score)
print("学生平均成绩:", average_score)
```
这段代码首先定义了一个空字典 `students`,然后通过循环输入每个学生的学号、姓名和成绩,并将它们存储在字典中。最后,用一个循环计算所有学生的总成绩,再除以学生人数得到平均成绩,并输出结果。
注意:这个示例代码没有进行输入的检查,如输入的学号是否重复、成绩是否在合理范围内等。在实际使用中需要根据具体情况进行完善。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)