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()
有什麼錯嗎?
你用'None'初始化'lstart'。現在,如果''CURRENT VALUE OF HEAT OF FORMATION''不在'lines [lindex]'中,那麼這個值永遠不會改變。 – Matthias
如果'len(lines)== 0',那麼你將永遠不會進入你的循環,並且永遠不會將'lstart'從'None'改變爲 –
當lstart到達for循環時它看起來仍然是None,它嘗試使用'+'運算符來加入'None'類型和'int' – CiaranWelsh