else statemnst always true, way?

Hi
I made a loop in a .txt file and it works as long as I it finds what it is looking for.
But when it not found what it is looking for in the .txt file I got an error.
And when I try wiht a ELSE statment, it Always return this even if the IF statement is true.


def findrow(num):
        modulfile_open = open(modulfile, 'r')      # open  file for reading
  for line in modulfile_open:
            values = line.split(",")
            if values[0] == num:
                newNum = values[1]
                print(values[1])                            #print to see that works
                modulfile_open.close()
                return newNum
                       
            else:
                newNum2 = ' Assembly not found'
                return newNum2       



Anyone that can tell me what is wrong?

//Wilnix

Maybe because you are comparing a number to string? Either convert the values into a number type or num into a string.


 if values[0] == str(num):

Also using a context manager for open/close the file.


def findrow(num):
    with open(modulfile, 'r') as modulefile_open:
        for line in modulfile_open:
            values = line.split(",")
            if values[0] == str(num): # of convert vales to
                newNum = values[1]
                return newNum                       
            else:
                newNum2 = ' Assembly not found'
                return newNum2

There is no need to explicitly close the file when you return.