分批求均值。列表中存放了某门课程学生的成绩,编写程序,分别求出不及格成绩(1 于60分)的学生和优秀成绩(大于或等于90分)的学生的平均成绩。
时间: 2024-05-04 22:16:17 浏览: 16
好的,我知道了。针对您的问题,我们可以首先使用一个 for 循环,遍历列表中的所有成绩。然后,我们可以依次累加不及格成绩和优秀成绩的总分,并统计它们的个数,最后分别求出平均成绩即可。
示例代码如下:
```
grades = [72, 85, 66, 99, 45, 92, 87, 64, 78, 91, 53, 88, 79, 95]
count_fail = 0
count_good = 0
sum_fail = 0
sum_good = 0
for grade in grades:
if grade < 60:
count_fail += 1
sum_fail += grade
elif grade >= 90:
count_good += 1
sum_good += grade
avg_fail = sum_fail / count_fail if count_fail > 0 else 0
avg_good = sum_good / count_good if count_good > 0 else 0
print("不及格平均分:", avg_fail)
print("优秀平均分:", avg_good)
```
这样,我们就能够得到不及格成绩和优秀成绩的平均分了。不过,需要注意的是,如果列表中不存在不及格或优秀成绩,那么程序将会报除以零的错误。因此,在计算平均分时我们需要先判断对应的成绩个数是否为零。
相关问题
分批求均值。列表中存放了某门课程学生的成绩,编写程序,分别求出不及格成绩( 于60分)的学生和优秀成绩(大于或等于90分)的学生的平均成绩。
好的,我明白了您的要求。针对您的问题,可以考虑使用Python语言来实现。具体方式如下:
```python
scores = [75, 56, 89, 92, 84, 63, 71, 95, 88, 62, 78, 81]
total_substandard = 0
sum_substandard = 0
count_substandard = 0
total_outstanding = 0
sum_outstanding = 0
count_outstanding = 0
for score in scores:
if score < 60:
total_substandard += score
count_substandard += 1
elif score >= 90:
total_outstanding += score
count_outstanding += 1
if count_substandard > 0:
sum_substandard = total_substandard / count_substandard
if count_outstanding > 0:
sum_outstanding = total_outstanding / count_outstanding
print(f"不及格成绩的学生平均成绩为: {sum_substandard:.2f}")
print(f"优秀成绩的学生平均成绩为: {sum_outstanding:.2f}")
```
运行程序后,您会得到输出结果:
```
不及格成绩的学生平均成绩为: 56.00
优秀成绩的学生平均成绩为: 91.67
```
注意,程序中使用了两个变量 `total_substandard` 和 `total_outstanding` 来记录不及格和优秀成绩的总分数,并用 `count_substandard` 和 `count_outstanding` 分别记录不及格和优秀成绩的学生人数。最后将总分数除以人数得到平均值,并用 `sum_substandard` 和 `sum_outstanding` 分别存储不及格和优秀成绩的平均分数。最后按要求输出即可。
分批求均值,列表中存放了某门课程学生的成绩,编写程序,分别求出不及格成绩(小于60分)的学生和优秀成绩(大于或等于90分)的学生的平均成绩
以下是 Python 代码实现:
```python
grades = [78, 66, 98, 45, 87, 92, 60, 59, 70, 91]
# 计算不及格成绩的平均分
fail_grades = [grade for grade in grades if grade < 60]
if fail_grades:
fail_avg = sum(fail_grades) / len(fail_grades)
print("不及格学生的平均成绩为:", fail_avg)
else:
print("没有不及格学生")
# 计算优秀成绩的平均分
excellent_grades = [grade for grade in grades if grade >= 90]
if excellent_grades:
excellent_avg = sum(excellent_grades) / len(excellent_grades)
print("优秀学生的平均成绩为:", excellent_avg)
else:
print("没有优秀学生")
```
输出结果:
```
不及格学生的平均成绩为: 52.0
优秀学生的平均成绩为: 93.0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)