python报错‘NoneType‘ object has no attribute ‘append‘
defcalc(*numbers):#定义一个int类型的常量,用以接收结果sum=0#定义一个空集合,用已接收数组中每一个数字的次方l=[]forninnumbers:sum=n*n#l=l.append(sum)不能这样写,报错#'NoneType'objecthasnoattribute'append'#因为appen...
·
def calc(*numbers):
#定义一个int类型的常量,用以接收结果
sum = 0
#定义一个空集合,用已接收数组中每一个数字的次方
l = []
for n in numbers:
sum = n * n
#l = l.append(sum)不能这样写,报错
#'NoneType' object has no attribute 'append'
#因为append会修改l本身,并返回None。不能把返回值在赋值给l
#把得到的结果依次放入集合中
l.append(sum)
return l
#注:calc中赋值时只能是int类型,不要加中括号,会把类型变成集合并报错
print(calc(1,2,3,3,65,47,4,756,756,75,63543,45))
更多推荐
已为社区贡献3条内容
所有评论(0)