目的是输入一个列表x后,如果x中全为数字,则输出大于其平均值的数的平方,如果不全是数字就输出列表中长度大于2的元素
问题相关代码
class mf:
def myaver(x):
y =[]
aver = sum(x)/len(x)
for i in x:
if i >= aver:
y.append(i)
x = y
def is_number(x):
try:
float(x)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError,ValueError):
pass
return False
def af(x):
'''
x is a list
'''
flag = True
for i in x:
if mf.is_number(i) == True:
pass
else:
flag = False
if flag == True:
y = list(filter(mf.myaver,x))
z = list(map(y**2,y))
print(z)
else:
print(list(filter(lambda i:len(i)>=2,x)))
运行结果及报错内容
aver = sum(x)/len(x)
TypeError: 'int' object is not iterable