Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现此错误(非类型)?_Python - Fatal编程技术网

Python 为什么会出现此错误(非类型)?

Python 为什么会出现此错误(非类型)?,python,Python,我知道有关于NoneType错误的帮助论坛。但是为了我的爱,我不能保存我的代码 counter=0 agesList=[] for line in database: newlist=database[counter].split(',') counter=counter+1 age=newlist[-2] agesList=agesList.append(age) #print(age) print(agesList) 我有一个由逗号分隔的值组成的文档

我知道有关于
NoneType
错误的帮助论坛。但是为了我的爱,我不能保存我的代码

counter=0
agesList=[]
for line in database:
    newlist=database[counter].split(',')
    counter=counter+1
    age=newlist[-2]
    agesList=agesList.append(age)
    #print(age)
print(agesList)
我有一个由逗号分隔的值组成的文档,我将它们都分割到自己的列表中,并取出我要查找的编号,该编号位于
新列表
的倒数第二位。如果我打印年龄,我会在整个循环中得到所有正确的年龄。但如果我试图将所有年龄数字附加到我的空白列表中,我会出现以下错误:

agesList=agesList.append(age)
AttributeError: 'NoneType' object has no attribute 'append'

我能得到一些关于我为什么会出现这个错误的帮助吗?谢谢

您正在为列表分配一个函数调用,这就是您正在覆盖列表的原因

不要这样做:

agesList=agesList.append(age)
只需这样做:

agesList.append(age)

您正在为列表分配函数调用,这就是您覆盖列表的原因

不要这样做:

agesList=agesList.append(age)
只需这样做:

agesList.append(age)

不要使用
agesList=agesList.append(age)
仅使用
agesList.append(age)

因为
list.append()
返回
None
并覆盖列表


因此,for循环的第一次迭代将正常运行,之后
agesList
None
,您将得到您提到的错误。

而不是
agesList=agesList.append(age)
仅使用
agesList.append(age)

因为
list.append()
返回
None
并覆盖列表

因此,for循环的第一次迭代将正常运行,之后
agesList
None
,您将得到您提到的错误