Problem p02825 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n = int(input())
pata = ["abb", 
        "a.c", 
        "ddc"]

patb = ["abcc",
        "abdd",
        "eefg",
        "hhfg"]

patc = ["abbcc",
        "ad..e",
        "fd..e",
        "f.ggh",
        "iijjh"]

patd = [".aabbcc",
        "a.ddee.",
        "ad....d",
        "bd....d",
        "be....e",
        "ce....e",
        "c.ddee."]

pate = [".aabbcc....",
        "a.ddee.....",
        "ad....d....",
        "bd....d....",
        "be....e....",
        "ce....e....",
        "c.ddee.....",
        ".......abcc",
        ".......abdd",
        ".......ccab",
        ".......ddab"]

cnt = 0

def create_ab(w):
    val = [["."] * w for _ in range(w)]
    ok = False
    for fives in range(200):
        if (w - fives * 5) % 4 == 0:
            fours = (w - fives * 5) // 4
            if fours >= 0:
                ok = True
                break
    if not ok:
        return None
    t = 0
    for i in range(fives):
        for i, line in enumerate(patc):
            for j, ch in enumerate(line):
                val[t+i][t+j] = ch
        t += 5
    for i in range(fours):
        for i, line in enumerate(patb):
            for j, ch in enumerate(line):
                val[t+i][t+j] = ch
        t += 4 
    ret = []
    for line in val:
        ret.append("".join([str(item) for item in line]))
    return ret

def solve(n):
    global cnt
    if n % 3 == 0:
        repeat = n // 3
        for i in range(repeat):
            for line in pata:
                print(line * repeat)
    elif n % 4 == 0:
        repeat = n // 4
        for i in range(repeat):
            for line in patb:
                print(line * repeat)
    elif n % 5 == 0:
        repeat = n // 5
        for i in range(repeat):
            for line in patc:
                print(line * repeat)
    elif n % 7 == 0:
        repeat = n // 7
        for i in range(repeat):
            for line in patd:
                print(line * repeat)
    elif n % 11 == 0:
        repeat = n // 11
        for i in range(repeat):
            for line in pate:
                print(line * repeat)
    else:
        ret = create_ab(n)
        if ret:
            for line in ret:
                print(line)
        else:
            cnt += 1
            print(-1)

solve(n)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n = int(input())
pata = ["abb", 
        "a.c", 
        "ddc"]

patb = ["abcc",
        "abdd",
        "eefg",
        "hhfg"]

patc = ["abbcc",
        "ad..e",
        "fd..e",
        "f.ggh",
        "iijjh"]

def create_ab(w):
    val = [["."] * w for _ in range(w)]
    ok = False
    for fives in range(200):
        if (w - fives * 5) % 4 == 0:
            fours = (w - fives * 5) // 4
            ok = True
            break
    if not ok:
        return None
    t = 0
    for i in range(fives):
        for i, line in enumerate(patc):
            for j, ch in enumerate(line):
                val[t+i][t+j] = ch
        t += 5
    for i in range(fours):
        for i, line in enumerate(patb):
            for j, ch in enumerate(line):
                val[t+i][t+j] = ch
        t += 4 
    ret = []
    for line in val:
        ret.append("".join([str(item) for item in line]))
    return ret

if n % 3 == 0:
    repeat = n // 3
    for i in range(repeat):
        for line in pata:
            print(line * repeat)
elif n % 4 == 0:
    repeat = n // 4
    for i in range(repeat):
        for line in patb:
            print(line * repeat)
elif n % 5 == 0:
    repeat = n // 5
    for i in range(repeat):
        for line in patc:
            print(line * repeat)
else:
    ret = create_ab(n)
    if ret:
        for line in ret:
            print(line)
    else:
        print(-1)