Python:TypeError:不支持+:“NoneType”和“int”的操作数类型

2024-04-26 19:07:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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'


Tags: runimporttxtformatfortimevalstart