无法理解python错误

时间:2017-06-17 22:16:49

标签: 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()

有什么不对吗?

1 个答案:

答案 0 :(得分:0)

此脚本似乎存在一些潜在问题。

  1. 在您描述的错误点,似乎lstart仍然存在 分配给值None,因此无法将其添加到值4。
  2. 如注释中所述,函数out_to_mop_let()不会被调用,因此某些变量(即行)不会被赋值任何值。
  3. 行变量对你的脚本至关重要......你的if语句都依赖于行来做出决定。