[英]Python error "TypeError: object of type 'NoneType' has no len()
I am getting an error from this python code 我从这个python代码中得到一个错误
def read_lines():
user_entries = []
contin = True
while contin == True:
user_input = input(str("Enter string or just press enter to finish: "))
user_entries.append(user_input)
if len(user_input) == 0:
contin = False
print(user_entries)
lines = read_lines()
print(lines)
ci = o
contin = True
while contin:
if ci == len(lines):
contin = False
else:
line = lines(ci, ci + 1)
ci = ci + 1
print(ci, line)
I then get this error... 然后我得到这个错误...
Traceback (most recent call last):
File "test.py", line 8 in <module>
if ci == len(lines):
Type Error: object of type 'NoneType' has no len()
I'm not quite sure why i get this error. 我不太确定为什么会收到此错误。 When it executes the line print(lines) it returns None.
当执行行print(lines)时,它返回None。 That's probably the issue but I'm not sure how to fix it.
这可能是问题所在,但我不确定如何解决。
Your function read_lines
doesn't return anything 您的函数
read_lines
不返回任何内容
def read_lines():
user_entries = []
contin = True
while contin == True:
user_input = input(str("Enter string or just press enter to finish: "))
user_entries.append(user_input)
if len(user_input) == 0:
contin = False
print(user_entries) # this prints user_entries but doesn't return it
Add a return user_entries
to your function after the call to print
and your code should work 在调用
print
之后,将return user_entries
添加到您的函数中,您的代码应该可以正常工作
The function you defined read_lines()
does not return anything to lines
so it is None . 您定义的函数
read_lines()
不会向lines
返回任何内容,因此它为None 。
In your function add return after printing. 在您的函数中添加打印后的返回。
return user_entries
Also you need do changes in your code such as line = lines(ci, ci + 1)
您还需要在代码中进行更改,例如
line = lines(ci, ci + 1)
lines
is a list: it's not callable. lines
是一个列表:不可调用。
ci = 0
instead of ci = o
ci = 0
而不是ci = o
Please remove the typos, errors and try again. 请清除输入错误,错误,然后重试。 Should work.
应该管用。
I have executed this code using python 2.7 我已经使用python 2.7执行了此代码
-- > function read_lines()
does not return anything that is one thing. ->函数
read_lines()
不返回任何一件事。
but inside the while loop, instead of using : 但在while循环内,而不是使用:
user_input = input(str("Enter string or just press enter to finish: "))
Use : 采用 :
user_input = raw_input("Enter string or just press enter to finish: ")
So you will not have next errors. 这样您就不会有下一个错误。 ;)
;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.