我程序的函数python .extend()出了点问题 [英] something wrong with my program's function python .extend()
问题描述
当我在python中为程序创建函数时,遇到了一个问题.每次我运行程序时,输入所需的内容后,我总是收到一条错误消息:"AttributeError:'NoneType'对象没有属性'extend'"该程序是:
while I was creating a function for a program in python, I encountered a problem. every time I run the program, after inputting what's needed I always get an Error saying: "AttributeError: 'NoneType' object has no attribute 'extend'" the program is:
def getDoubleList (generalPrompt, sentinel):
END="not end"
OUT= []
print(generalPrompt)
while END.upper() != sentinel.upper():
IN=input(">")
END=IN.upper()
if IN.upper() != sentinel.upper():
IN=list((IN).split(" "))
OUT=(OUT.extend(IN))
return OUT
#testing part (no editing below this point)
nums = getDoubleList("Enter a list of numbers:", "end")
print("Your numbers (sorted):")
for n in sorted(nums):
print(n)
当我运行它时,我得到以下信息:
And when i run it i get the following:
Enter a list of numbers:
>2 3
>3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
[...]
nums = getDoubleList("Enter a list of numbers:", "end")
File "C:/Users/admin/Downloads/getDoubleDriver.py", line 12, in getDoubleList
OUT=(OUT.extend(IN))
AttributeError: 'NoneType' object has no attribute 'extend'
但是我的函数中没有任何nonetypes.我不知道尝试查找问题的功能出了什么问题,但没有
however I don't have any nonetypes within my function. I don't know what went wrong in the function i tried to look for the problem but without
推荐答案
.extend
是就地方法,它不返回任何内容,它会修改您的列表.
.extend
is in-place method, it does not return anything, it modifies your list.
查看示例
>>> print [].extend([3])
None
或
>>> x = []
>>> print x
[]
>>> y = x.extend([4])
>>> print y
None
>>> print x
[4]
因此要解决问题,只需更改
thus to solve the problem, simply change
OUT=(OUT.extend(IN))
到
OUT.extend(IN)
或等价
OUT = OUT + IN
这篇关于我程序的函数python .extend()出了点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!