python报错:AttributeError: 'NoneType' object has no attribute 'append'
程序员文章站
2022-03-26 19:31:58
...
源程序:
'''
Rewrite the program that prompts the user for a list of numbers
and prints out the maximum and minimum of the numbers at the end when the user enters "done".
Write the program to store the numbers the user enters in a list
and use the max() and min() functions
to compute the maximum and minimum numbers after the loop completes.
'''
list = list() #建立一个空列表
while True:
str = input('Enter a number: ')
if str == 'Done':
break
try:
num = float(str)
except:
print('You enter the wrong number!')
quit()
#print(num)
list = list.append(num)
print(list)
报错:
Traceback (most recent call last):
File “E:/TESTS/PYTHON/list_ex_03/list_ex_03.py”, line 20, in
list = list.append(num)
AttributeError: ‘NoneType’ object has no attribute ‘append’
原因:
list = list.append(num),由于列表本身是可以被改变的,append()改变了列表并且返回None,list = None会报错。
解决方法:
修改为:
list.append(num)
即可。
推荐阅读
-
解决Keras报错AttributeError: 'NoneType' object has no attribute 'inbound_nodes'
-
AttributeError: ‘NoneType‘ object has no attribute ‘origin‘解决办法
-
AttributeError: ‘set‘ object has no attribute ‘append‘解决办法
-
使用torchsummary时报错AttributeError: ‘list‘ object has no attribute ‘size‘
-
Python AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'
-
Python 爬取网页信息 AttributeError :’NoneType’ object has no attribute ’attrs’
-
python报错:AttributeError: 'module' object has no attribute 'xfeatures2d'
-
python易错点 | AttributeError: 'xxxx' object has no attribute 'xxxx'
-
wsgiref报错AttributeError: 'NoneType' object has no attribute 'split'
-
反向关联报错AttributeError: ‘ForeignKey‘ object has no attribute ‘rel‘从django源码找答案~