Search code examples
pythonpython-2.7while-loopnonetype

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' in while loop


There is a function to match a string with the items in a list, which returns the index number of an list item if there is a match. Like below:

def get_int(get_wd, get_list):
   for i, j in enumerate(get_list):
       if j == get_wd:
         get_i = i
         return get_i

And there is a while-loop in the main function to obtain the return integer from above function:

get_wd = []
x = 0
candi = []

while len(li_a) > 0:
    iter_a = iter(li_a)
    srh_time = len(li_a)
    while srh_time > 0:
        temp = next(iter_a)
        if temp in li_words:
            candi.append(temp)
        else:
            pass
        srh_time = srh_time - 1
    max_len = max(len(s) for s in candi)
    extr_wd = list(set(s for s in candi if len(s) == max_len))
    pos = get_int(extr_wd, li_a) ##Calling the function##
    get_wd.append(extr_wd)
    li_a = li_a[pos + 1:]

I'm getting this error message:

>> li_a = li_a[pos + 1:]
>> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Any advice for what I'm missing?


Solution

  • I think get_int is expecting str or int as the first list as the second argument, but pos = get_int(extr_wd, li_a) here both arguments are list, you should fix this.

    you can use .index for finding the index

    refactored get_int method:

    def get_int(get_wd, get_list):
         try:
             return get_list.index(get_wd)
         except ValueError:
             return -1 # not found