基本统计值计算,输入自己的五次实验成绩,产生一个列表,求五次成绩的平均值,中位数全部用函数完成
时间: 2023-12-19 20:05:47 浏览: 23
以下是Python代码实现:
```python
# 输入五次实验成绩
scores = []
for i in range(5):
score = float(input("请输入第{}次实验成绩:".format(i+1)))
scores.append(score)
# 计算平均值
def average(scores):
return sum(scores) / len(scores)
avg_score = average(scores)
print("平均成绩为:", avg_score)
# 计算中位数
def median(scores):
scores.sort()
n = len(scores)
if n % 2 == 0:
return (scores[n//2-1] + scores[n//2]) / 2
else:
return scores[n//2]
med_score = median(scores)
print("中位数为:", med_score)
```
输出示例:
```
请输入第1次实验成绩:85
请输入第2次实验成绩:90
请输入第3次实验成绩:75
请输入第4次实验成绩:80
请输入第5次实验成绩:88
平均成绩为: 83.6
中位数为: 85.0
```
相关问题
基本统计值计算,输入自己的五次实验成绩,产生一个列表,求五次成绩的平均值,中位数,标准差,参照课本p164,用四个函数完成,从输入,到计算全部用函数完成。
以下是实现这个功能的代码,可以供参考:
```python
def input_scores():
scores = []
for i in range(5):
score = float(input("请输入第{}次实验成绩:".format(i+1)))
scores.append(score)
return scores
def mean(scores):
total = sum(scores)
return total / len(scores)
def median(scores):
sorted_scores = sorted(scores)
n = len(scores)
if n % 2 == 0:
return (sorted_scores[n//2-1] + sorted_scores[n//2]) / 2
else:
return sorted_scores[n//2]
def variance(scores):
n = len(scores)
avg = mean(scores)
deviations = [(x - avg)**2 for x in scores]
return sum(deviations) / (n - 1)
def std_deviation(scores):
return variance(scores) ** 0.5
scores = input_scores()
print("五次实验成绩为:", scores)
print("平均值为:{:.2f}".format(mean(scores)))
print("中位数为:{:.2f}".format(median(scores)))
print("标准差为:{:.2f}".format(std_deviation(scores)))
```
运行代码后,会提示输入五次实验成绩,然后分别计算平均值、中位数和标准差,并将结果输出。
基本统计值计算,包括求总数个数、求和、平均值、方差、中位数。
假设有一组数据:{10, 15, 20, 25, 30},下面是各种基本统计值的计算方法:
1. 总数个数:该组数据的总数为 5。
2. 求和:将 5 个数相加,得到 100。
3. 平均值:将 5 个数相加,再除以总数个数,得到 20。
4. 方差:先计算每个数据与平均值的差,然后平方,再将这些平方值相加,最后再除以总数个数。具体计算方法如下:
(10-20)² + (15-20)² + (20-20)² + (25-20)² + (30-20)² = 250
250 ÷ 5 = 50,所以该组数据的方差为 50。
5. 中位数:将数据按大小顺序排列,然后找到中间的数。如果数据个数为奇数,则中位数为排序后的中间的数;如果数据个数为偶数,则中位数为排序后中间两个数的平均值。对于这组数据,排序后为 {10, 15, 20, 25, 30},因此中位数为 20。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)