IngenieraEnApuros
IngenieraEnApuros

Reputation: 3

Python: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

I am a begginer in Python and I can not find the mistake in this code bellow. Do you know how I can fix it? Thanks!

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 Traceback (most recent call last) in ()

     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'

Upvotes: 0

Views: 4322

Answers (1)

Barmar
Barmar

Reputation: 780974

I suspect your search() function returns None when it can't find the pattern, not -1. So change

if val == -1:

to

if val is None:

Upvotes: 1

Related Questions