2017-06-17 146 views
1

我正在处理一个不起作用的脚本。无法理解python错误

首先我抄写错误:

File "/home/path", line nº, in out_to_mop_let #~this is the function 
    for lindex in range(lstart+ 4,lfinish): 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 

的脚本是下面的一个,在那里我已经把周围的明显的问题**符号:

def out_to_mop_let (namefilein, namefileout, keywords=""): 
    print namefilein 
    filein=open(namefilein, "r") 
    fileout=open(namefileout, "w") 
    lines = filein.readlines() 
    filein.close() 

lstart = None 
lfinish = None 

for lindex in range(0,len(lines)): 
    if lindex-4>lstart and lines[lindex]=='\n' and lfinish==None and lstart!=None: 
     lfinish=lindex+1 
    if "CURRENT VALUE OF HEAT OF FORMATION" in lines[lindex]: 
     lstart=lindex 
print lstart 
print lfinish 
fileout.write(keywords + "\n\n\n") 
**for lindex in range(lstart+ 4,lfinish): 
    fileout.write(lines[lindex])** 

fileout.close() 
filein.close() 

有什么错吗?

+0

你用'None'初始化'lstart'。现在,如果''CURRENT VALUE OF HEAT OF FORMATION''不在'lines [lindex]'中,那么这个值永远不会改变。 – Matthias

+0

如果'len(lines)== 0',那么你将永远不会进入你的循环,并且永远不会将'lstart'从'None'改变为 –

+0

当lstart到达for循环时它看起来仍然是None,它尝试使用'+'运算符来加入'None'类型和'int' – CiaranWelsh

回答

0

这个脚本似乎有几个潜在的问题。

  1. 在你描述的错误点,看来lstart仍然 分配给值无,因此它不能被添加到值4
  2. 正如评论指出,该功能out_to_mop_let()不会被调用,所以一些变量,即行不会被分配任何值。
  3. 行变量对脚本至关重要......您的if语句都依赖行来做出决定。