How to resolve IndexError: list index out of range python?

  python, python-3.x

I am trying to print front-slashes and back-slashes based on list values but I can not understand how it is getting the out of range

def showSlashes(nums, lines):
    su = 0
    sd = 0
    li =-1
    
    for num in nums:
        for x in range(num):
            if x%2 != 0:
                li += 1
                lines[li] += (" " *su+"/")
                su = len(lines[li])-len(lines[li+1])
            else:
                lines[li] += (" " *sd+"")
                try:
                    sd = len(lines[li])-len(lines[li -1])
                except:
                    pass
                li -= 1
                su = 0
    print ('n'.join(lines[::-1]))

nums = [2,3,1,2,3,1,2,1,2]
lines = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

showSlashes(nums, lines)

I am getting error IndexError: list index out of range because of lines[li] += (" " *sd+"")

Source: Python-3x Questions

LEAVE A COMMENT