编译器不断抛出AttributeError:“'NoneType'对象没有属性'append'”

时间:2016-12-16 23:27:17

标签: python for-loop append mean

我正在编写一个程序,它接受几个数字的输入,然后将输入的数字放在一个列表中。然后程序找到并输出列表中所有数字的平均值到控制台。每当我运行此程序时,我都会收到错误AttributeError: 'NoneType' object has no attribute 'append'

导致此错误的原因是什么?

episode_list= []

mather= input("Enter list:")

for number in mather:
    episode_list= episode_list.append(number)

for element in episode_list:
    total += element

final= total/ len(episode_list)

print(final)

2 个答案:

答案 0 :(得分:2)

使用以下命令更新您的第一个for循环:

for number in mather:
    episode_list.append(number)

list.appendlist上执行追加操作并返回None

此外,在您的第二个for循环中,您需要执行以下操作:

for element in episode_list:
    total += int(element)
    #        ^ Type-cast the value to `int` type 

答案 1 :(得分:1)

episode_list.append(number)就足够了

这是因为list.append是就地完成的。