Python: TypeError: object of type 'NoneType' has no len()

前端 未结 4 1238
-上瘾入骨i
-上瘾入骨i 2021-02-14 15:56

I am getting an error from this Python code:

with open(\'names\') as f:
    names = f.read()
    names = names.split(\'\\n\')
    names.pop(len(names) - 1)
    n         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 16:42

    What is the purpose of this

     names = list;
    

    ? Also, no ; required in Python.

    Do you want

     names = []
    

    or

     names = list()
    

    at the start of your program instead? Though given your particular code, there's no need for this statement to create this names variable since you do so later when you read data into it from your file.

    @JBernardo has already pointed out the other (and more major) problem with the code.

提交回复
热议问题