Problem p02903 (python)

Fixed submission:
from sys import stdin
h,w,b,a = map(int,stdin.readline().rstrip().split())
li = []
for i in range(a):
    s = "0"*b+"1"*(w-b)
    li.append(s)
for j in range(h-a):
    s = "1"*b+"0"*(w-b)
    li.append(s)
for i in li:
    print(i)

Buggy submission (verdict: Runtime Error):
from sys import stdin

h,w,a,b = [int(x) for x in stdin.readline().rstrip().split()]

li = [[0 for _ in range(w)]for _ in range(h)]

if a == 0:
    a = 1
if b == 0:
    b = 1

if h%b != 0 or w%a != 0:
    if h == 1 or w == 1:
        count = 0
        for i in range(h):
            for s in range(a):
                li[i][s+a*count] = 1
            count += 1
            count = count%(h/a)

        li2 = [li[q][0] for q in range(h)]

        if li2.count(0) == b or li2.count(1) == b:
            for z in range(h):
                print(''.join(map(str, li[z])))
                exit()
        else:
            print("No")
            exit()
    else:    
        print("No")
        exit()

if a > h//2 or b > w//2:
    print("No")
    exit()

count = 0
for i in range(h):
    for s in range(a):
        li[i][s+a*count] = 1
    count += 1
    count = count%(h/a)

li2 = [li[q][0] for q in range(h)]

if li2.count(0) == b or li2.count(1) == b:
    for z in range(h):
        print(''.join(map(str, li[z])))
        exit()
else:
    print("No")
    exit()

Buggy submission (verdict: Runtime Error):
from sys import stdin

h,w,a,b = [int(x) for x in stdin.readline().rstrip().split()]

li = [[0 for _ in range(w)]for _ in range(h)]
if a > w//2 or b > h//2:
    print("No")
    exit()
elif h%a != 0 or w%b != 0:
    if a != 0 and b != 0:
        print("No")
        exit()
    else:
        if a == 0:
            a = 1
        if b == 0:
            b = 1
        count = 0
        for i in range(h):
            for s in range(a):
                li[i][s+a*count] = 1
            count += 1
            count = count%(h/a)
        for p in range(h):
            print(''.join(map(str, li[p])))
elif w%h != 0 and h%w != 0:
    print("No")
    exit()
else:
    count = 0
    for i in range(h):
        for s in range(a):
            li[i][s+a*count] = 1
        count += 1
        count = count%(h/a)
    li2 = [li[q][0] for q in range(h)]
    if li2.count(0) == b or li2.count(1) == b:
        for p in range(h):
            print(''.join(map(str, li[p])))
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
from sys import stdin

h,w,a,b = [int(x) for x in stdin.readline().rstrip().split()]

li = [[0 for _ in range(w)]for _ in range(h)]
if a > w//2 or b > h//2:
    print("No")
    exit()
elif h%a != 0 or w%b != 0:
    if a != 0 and b != 0:
        print("No")
        exit()
    else:
        if a == 0:
            a = 1
        elif b == 0:
            b = 1
        count = 0
        for i in range(h):
            for s in range(a):
                li[i][s+a*count] = 1
            count += 1
            count = count%(h//a)
        for p in range(h):
            print(''.join(map(str, li[p])))
elif w%h != 0 and h%w != 0:
    print("No")
    exit()
else:
    count = 0
    for i in range(h):
        for s in range(a):
            li[i][s+a*count] = 1
        count += 1
        count = count%(h//a)
    li2 = [li[q][0] for q in range(h)]
    if li2.count(0) == b or li2.count(1) == b:
        for p in range(h):
            print(''.join(map(str, li[p])))
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
from sys import stdin

h,w,a,b = [int(x) for x in stdin.readline().rstrip().split()]

li = [[0 for _ in range(w)]for _ in range(h)]

if a > w//2 or b > h//2:
    print("No")
    exit()
elif h%a != 0 or w%b != 0:
    if a != 0 and b != 0:
        print("No")
        exit()
    else:
        if a == 0:
            a = 1
        elif b == 0:
            b = 1
        count = 0
        for i in range(h):
            for s in range(a):
                li[i][s+a*count] = 1
            count += 1
            count = count%(h//a)
        for p in range(h):
            print(''.join(map(str, li[p])))
else:
    count = 0
    for i in range(h):
        for s in range(a):
            li[i][s+a*count] = 1
        count += 1
        count = count%(h//a)
    li2 = [li[q][0] for q in range(h)]
    if li2.count(0) == b or li2.count(1) == b:
        for p in range(h):
            print(''.join(map(str, li[p])))
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
from sys import stdin

h,w,a,b = [int(x) for x in stdin.readline().rstrip().split()]

li = [[0 for _ in range(w)]for _ in range(h)]

if a > w//2 or b > h//2:
    print("No")
    exit()
elif h%a != 0 or w%b != 0:
    if a != 0 and b != 0:
        print("No")
        exit()
    else:
        if a == 0:
            a = 1
        elif b == 0:
            b = 1
        p = 0
        for i in range(h):
            for s in range(a):
                li[i][p*a+s] = 1
                p += 1
        for p in range(h):
            print(''.join(map(str, li[p])))
else:
    p = 0
    for i in range(h):
        for s in range(a):
            li[i][p*a+s] = 1
            p += 1
    li2 = [li[q][0] for q in range(h)]
    if li2.count(0) == b or li2.count(1) == b:
        for p in range(h):
            print(''.join(map(str, li[p])))
    else:
        print("No")