我正在编写一个程序,它接受几个数字的输入,然后将输入的数字放在一个列表中。然后程序找到并输出列表中所有数字的平均值到控制台。每当我运行此程序时,我都会收到错误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)
答案 0 :(得分:2)
使用以下命令更新您的第一个for
循环:
for number in mather:
episode_list.append(number)
list.append
在list
上执行追加操作并返回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是就地完成的。