Problem p02825 (python)

Fixed submission:
N = int(input())

lst = [['.'] * N for _ in range(N)]

def calc3(x, y): #基準x, yから3×3マスを塗る
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x][y + 2] = 'b'
    lst[x + 1][y + 2] = 'b'

    lst[x + 2][y + 2] = 'c'
    lst[x + 2][y + 1] = 'c'

    lst[x + 2][y] = 'd'
    lst[x + 1][y] = 'd'

if N % 3 == 0:
    for h in range(0, N, 3):
        for w in range(0, N, 3):
            calc3(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc4(x, y):
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x + 1][y] = 'b'
    lst[x + 1][y + 1] = 'b'

    lst[x + 2][y + 2] = 'c'
    lst[x + 2][y + 3] = 'c'


    lst[x + 3][y + 2] = 'd'
    lst[x + 3][y + 3] = 'd'

    lst[x + 2][y] = 'e'
    lst[x + 3][y] = 'e'

    lst[x + 2][y + 1] = 'f'
    lst[x + 3][y + 1] = 'f'

    lst[x][y + 2] = 'g'
    lst[x + 1][y + 2] = 'g'

    lst[x][y + 3] = 'h'
    lst[x + 1][y + 3] = 'h'

if N % 4 == 0:
    for h in range(0, N, 4):
        for w in range(0, N, 4):
            calc4(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc5(x, y):
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x][y + 2] = 'b'
    lst[x][y + 3] = 'b'

    lst[x][y + 4] = 'c'
    lst[x + 1][y + 4] = 'c'


    lst[x + 1][y] = 'd'
    lst[x + 2][y] = 'd'

    lst[x + 1][y + 1] = 'e'
    lst[x + 2][y + 1] = 'e'

    lst[x + 2][y + 4] = 'f'
    lst[x + 3][y + 4] = 'f'

    lst[x + 3][y] = 'g'
    lst[x + 4][y] = 'g'

    lst[x + 3][y + 2] = 'h'
    lst[x + 3][y + 3] = 'h'

    lst[x + 4][y + 1] = 'i'
    lst[x + 4][y + 2] = 'i'

    lst[x + 4][y + 3] = 'j'
    lst[x + 4][y + 4] = 'j'

if N % 5 == 0:
    for h in range(0, N, 5):
        for w in range(0, N, 5):
            calc5(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc7(x, y):
    lst[x][y + 1] = 'a'
    lst[x][y + 2] = 'a'

    lst[x][y + 3] = 'b'
    lst[x][y + 4] = 'b'

    lst[x][y + 5] = 'c'
    lst[x][y + 6] = 'c'

    lst[x + 1][y] = 'd'
    lst[x + 2][y] = 'd'

    lst[x + 1][y + 3] = 'e'
    lst[x + 1][y + 4] = 'e'

    lst[x + 1][y + 5] = 'f'
    lst[x + 1][y + 6] = 'f'

    lst[x + 3][y] = 'g'
    lst[x + 4][y] = 'g'

    lst[x + 3][y + 1] = 'h'
    lst[x + 4][y + 1] = 'h'

    lst[x + 3][y + 2] = 'i'
    lst[x + 4][y + 2] = 'i'

    lst[x + 5][y] = 'j'
    lst[x + 6][y] = 'j'

    lst[x + 5][y + 1] = 'k'
    lst[x + 6][y + 1] = 'k'

    lst[x + 5][y + 2] = 'l'
    lst[x + 6][y + 2] = 'l'
    
    lst[x + 2][y + 3] = 'm'
    lst[x + 2][y + 4] = 'm'

    lst[x + 2][y + 5] = 'n'
    lst[x + 2][y + 6] = 'n'

if N % 7 == 0:
    for h in range(0, N, 7):
        for w in range(0, N, 7):
            calc7(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

if N <= 2:
    print (-1)
    exit()

count = 0
n = N
while True:
    if n % 5 == 0:
        flag = True
        break
    if n % 7 == 0:
        flag = False
        break
    n -= 4
    count += 1

for i in range(count):
    calc4(4 * i, 4 * i)


if flag:
    for i in range(4 * count, N, 5):
        calc5(i, i)
else:
    for i in range(4 * count, N, 7):
        calc7(i, i)


for i in lst:
        print (*i, sep = '')

Buggy submission (verdict: Runtime Error):
N = int(input())

lst = [['.'] * N for _ in range(N)]

def calc3(x, y): #基準x, yから3×3マスを塗る
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x][y + 2] = 'b'
    lst[x + 1][y + 2] = 'b'

    lst[x + 2][y + 2] = 'c'
    lst[x + 2][y + 1] = 'c'

    lst[x + 2][y] = 'd'
    lst[x + 1][y] = 'd'

if N % 3 == 0:
    for h in range(0, N, 3):
        for w in range(0, N, 3):
            calc3(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc4(x, y):
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x + 1][y] = 'b'
    lst[x + 1][y + 1] = 'b'

    lst[x + 2][y + 2] = 'c'
    lst[x + 2][y + 3] = 'c'


    lst[x + 3][y + 2] = 'd'
    lst[x + 3][y + 3] = 'd'

    lst[x + 2][y] = 'e'
    lst[x + 3][y] = 'e'

    lst[x + 2][y + 1] = 'f'
    lst[x + 3][y + 1] = 'f'

    lst[x][y + 2] = 'g'
    lst[x + 1][y + 2] = 'g'

    lst[x][y + 3] = 'h'
    lst[x + 1][y + 3] = 'h'

if N % 4 == 0:
    for h in range(0, N, 4):
        for w in range(0, N, 4):
            calc4(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

if N == 5:
    print (Z)

if N == 7:
    while True:
        pass

print (-1)