我是Python中的乞丐,在下面的代码中找不到错误。你知道我怎么修吗?谢谢!你知道吗
import fileinput
import time
pat = "hello"
cout = 0
with fileinput.input(files=('packet.txt')) as f:
for line in f:
start_time = time.time()
val = search(txt, pat)
end_time = time.time()
run_time = (end_time - start_time)*1000000
if(val == -1):
print("No Text")
run_time = (end_time - start_time)*1000000
#print(" ---> Processing time: "'{0:.3f}'.format(run_time), "microseconds")
print(" ---> Processing time: "'{0:.3f}'.format(run_time), "microseconds")
else:
val = val + 1
print ('Pattern \"' + pat + '\" found at position',val + count)
run_time = (end_time - start_time)*1000000
print(" ---> Processing time: "'{0:.3f}'.format(run_time), "microseconds")
()中的TypeError回溯(最近一次调用)
16 print(" ---> Processing time: ",'{0:.3f}'.format(run_time), "microseconds")
17 else:
---> 18 val = val + 1
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
我怀疑您的
search()
函数在找不到模式时返回None
,而不是-1
。所以改变吧至
相关问题 更多 >
编程相关推荐