from math import sqrt def getNum(): def mean(numbers): def dev(numbers, mean): def median(numbers): def find_max(numbers): def find_min(numbers): numbers = getNum() mean_value = mean(numbers) deviation = dev(numbers, mean_value) median_value = median(numbers) max_value = find_max(numbers) min_value = find_min(numbers) assert mean_value == 4.2, "平均值计算错误" assert deviation == 2.6076809620810595, "方差计算错误" assert median_value == 5.0, "中位数计算错误" assert max_value == 7, "最大值计算错误" assert min_value == 1, "最小值计算错误" print("所有断言测试通过")那里错误
时间: 2024-03-18 10:40:45 浏览: 60
你提供的代码缺少了一些必要的函数定义和实现,我假设你的代码应该是下面这样的:
```python
from math import sqrt
def getNum():
nums = []
while True:
try:
x = input("请输入一个数字(按 q 结束输入):")
if x == "q":
break
nums.append(float(x))
except ValueError:
print("输入错误,请重新输入!")
return nums
def mean(numbers):
return sum(numbers) / len(numbers) if len(numbers) > 0 else None
def dev(numbers, mean):
return sqrt(sum([(x - mean)**2 for x in numbers]) / (len(numbers) - 1)) if len(numbers) > 1 else None
def median(numbers):
nums = sorted(numbers)
n = len(nums)
return (nums[n//2] if n % 2 == 1 else (nums[n//2-1] + nums[n//2])/2) if n > 0 else None
def find_max(numbers):
return max(numbers) if len(numbers) > 0 else None
def find_min(numbers):
return min(numbers) if len(numbers) > 0 else None
if __name__ == "__main__":
numbers = getNum()
mean_value = mean(numbers)
deviation = dev(numbers, mean_value)
median_value = median(numbers)
max_value = find_max(numbers)
min_value = find_min(numbers)
assert mean_value == 4.2, "平均值计算错误"
assert deviation == 2.6076809620810595, "方差计算错误"
assert median_value == 5.0, "中位数计算错误"
assert max_value == 7, "最大值计算错误"
assert min_value == 1, "最小值计算错误"
print("所有断言测试通过")
```
根据你提供的代码,我发现你没有在函数定义中加入函数体的语句,例如:`mean(numbers):` 后面的函数体应该是一个缩进的代码块,例如:
```python
def mean(numbers):
s = sum(numbers)
n = len(numbers)
return s/n
```
另外,你的代码中也没有对`getNum()`函数进行定义,也就是没有输入数字的方式。
综上所述,我提供了一个可运行的代码,你可以参考一下。
阅读全文