list_1 = []
num = input("请输入数字:")
while num != "":
list_1.append(num)
num = input("请输入数字:")
#上述5条代码是重点要理解:定义一个全局变量list_1
"""
求和
"""
def sum_1():
sum = 0
for i in list_1:
sum += eval(i)
return sum
"""
求长度
"""
def len_1():
count = 0
for i in list_1:
count = count + 1
lenth = count
return lenth
"""
平均数
"""
def averange():
aver = sum_1() / len_1()
return aver
print(averange())#float
"""
方差
"""
def fang_cha():
fc = 0
for i in list_1:#之所以不是list_2,是因为它是局部变量,若写list_2,则list_2是空列表
a = float(i)
b = a - averange()
# print(type(averange()))
fc += pow(b, 2) / len_1()#简化公式,学会分解问题!!!
print(fc)
print(fang_cha())
运行结果:
结果测试:
a = pow(2.2,2)+pow(1.2,2)+pow(1.8,2)+pow(4.8,2)+pow(3.2,2)
print(a)
运行结果:
42.800000000000004
42.8 / 5 = 8.56
>>>
没问题。
来源:https://blog.csdn.net/YJG7D314/article/details/102747112