TypeError 无法在居中平均算法中解压不可迭代的浮点对象 Python
TypeError cannot unpack non-iterable float object in centered average algorithm Python
我在 Python 中有一个 类型错误 来自 中心平均 算法。这是我的代码
def calc_centered_average(numbers):
sorted_list = sorted(numbers)
return sum(sorted_list[1:-1])/(len(numbers)-2)
# return subnumbers, centered_average
numbers = [1, 4, 5, 6, 100]
sublist, cavg = calc_centered_average(numbers)
print(f"The centered average of {numbers} is {cavg} (based on {sublist}).")
您的问题是因为您在函数中返回一个值而不是两个值。这是我认为您应该这样做的正确方法:
def calc_centered_average(numbers):
sorted_list = sorted(numbers)
subnumbers = sorted_list[1:-1]
centered_average = sum(subnumbers)/(len(numbers)-2)
return subnumbers, centered_average
numbers = [1, 4, 5, 6, 100]
sublist, cavg = calc_centered_average(numbers)
print(f"The centered average of {numbers} is {cavg} (based on {sublist}).")
我在 Python 中有一个 类型错误 来自 中心平均 算法。这是我的代码
def calc_centered_average(numbers):
sorted_list = sorted(numbers)
return sum(sorted_list[1:-1])/(len(numbers)-2)
# return subnumbers, centered_average
numbers = [1, 4, 5, 6, 100]
sublist, cavg = calc_centered_average(numbers)
print(f"The centered average of {numbers} is {cavg} (based on {sublist}).")
您的问题是因为您在函数中返回一个值而不是两个值。这是我认为您应该这样做的正确方法:
def calc_centered_average(numbers):
sorted_list = sorted(numbers)
subnumbers = sorted_list[1:-1]
centered_average = sum(subnumbers)/(len(numbers)-2)
return subnumbers, centered_average
numbers = [1, 4, 5, 6, 100]
sublist, cavg = calc_centered_average(numbers)
print(f"The centered average of {numbers} is {cavg} (based on {sublist}).")