IndexError: list index out of range 4

1

I'm doing a program that seeks functions throws me this problem:

while lista[a] != (' ' or '' or '\n' or '\t' ):
  

IndexError: list index out of range

The whole program is this:

def look_for_def (lista):
    i = 0
    a = 0
    func = ''
    defi = []
    while i < len(lista):
        if (lista[i] =='d') and (lista[i + 1] =='e') and (lista[i + 2] == 'f') \
        and (lista[i + 3] == ' '):
            a = i + 3
            while lista[a] == ' ':
                a += 1
            while lista[a] not in {' ', '' ,'\n', '\t'}:
                func += lista[a]
                a += 1
            defi.append(func)
            func = ''
        i += 1
    return defi

An entry text would be ['d', 'e', 'f', '', 'f', 'u', 'c', 't']

The text that should come out would be ['funct']

Thank you very much!

    
asked by H4LLORENTE 29.03.2018 в 13:57
source

1 answer

1

Good day, it worked for me in the following way, if you want to analyze it and see what I did differently, or use it in the same way. The 2 major differences you will notice is that I deliberately add a space to the list to be analyzed, and the other is that I always increased the same as a, so that the output is not adding blank spaces.

    # -*- coding: utf-8 -*-
"""
Created on Thu Mar 29 06:17:42 2018

@author: Xonem
"""

#x = [' ', 'd', 'e', 'f', ' ', ' ', 'p', 'e', 'p', 'e']
x = [' ', 'd', 'e', 'f', ' ', ' ', 'p', 'e', 'p', 'e','p', 'e', 'p', 'e','p', 'e', 'p', 'e']

def look_for_def (lista):
    lista.append(' ')
    i = 0
    a = 0
    func = ''
    defi = []
    while i < len(lista):
        if (lista[i] =='d') and (lista[i + 1] =='e') and (lista[i + 2] == 'f') \
        and (lista[i + 3] == ' '):
            a = i + 3
            i+=3
            while lista[a] == ' ':
                a += 1
                i+=1
            while lista[a] != (' ' or '' or '\n' or '\t'):

                func += lista[a]
                a += 1
                i+=1
                defi.append(func[len(func)-1])



        i += 1
    return defi

y= look_for_def(x)
print(y)
    
answered by 29.03.2018 / 15:48
source