Problem p03854 (python)

Fixed submission:
s=input()
ind =0
len_s=len(s)
s = s+ "AAAAAAAAA"

A = "dreamer"
B= "dream"
C= "erase"
D="eraser"

flag =True
while(1):
    if(s[ind] =="d"):
        if(s[ind + 5] =="d"):
            if(s[ind:ind+5]!=B):
                flag = False
                break
            else:
                ind+=5
        else:
            if(s[ind+5]!='e' and s[ind+5]!='A'):
                flag=False
                break
            if(s[ind+7] == "a" or ind + 7 >len_s):
                if(s[ind:ind+5]!=B):
                    flag = False
                    break
                else:
                    ind+=5

            else:
                if(s[ind:ind+7] !=A):
                    flag = False
                    break
                else:
                    ind+=7
    elif(s[ind]=="e"):
        if(s[ind+5]=="r"):
            if(s[ind:ind+6]!=D):
                flag = False
                break
            else:
                ind+=6

        else:
            if(s[ind:ind +5] !=C):
                flag =False
                break
            else:
                ind+=5
    else:
        flag=False
        break
    if(ind == len_s):
        break
    if(ind > len_s):
        flag=False
        break
if(flag):
    print("YES")
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
s=input()
ind =0
len_s=len(s)
s = s+ "zzzzzzzzzz"

A = "dreamer"
B= "dream"
C= "erase"
D="eraser"

flag =True
while(1):
    if(s[ind] =="d"):
        if(s[ind + 5] =="d"):
            if(s[ind:ind+5]!=B):
                flag = False
                break
            else:
                ind+=5
        else:
            if(s[ind+5]!='e'):
                flag=False
                break
            if(s[ind+7] == "a"):
                if(s[ind:ind+5]!=B):
                    flag = False
                    break
                else:
                    ind+=5

            else:
                if(s[ind:ind+7] !=A):
                    flag = false
                    break
                else:
                    ind+=7
    elif(s[ind]=="e"):
        if(s[ind+5]=="r"):
            if(s[ind:ind+6]!=D):
                flag = False
                break
            else:
                ind+=6

        else:
            if(s[ind:ind +5] !=C):
                flag =False
                break
            else:
                ind+=5
    else:
        flag=False
        break
    if(ind == len_s):
        break
    if(ind > len_s):
        flag=False
        break
if(flag):
    print("YES")
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
s=input()
ind =0
len_s=len(s)
s = s+ "zzzzzzzzzz"

A = "dreamer"
B= "dream"
C= "erase"
D="eraser"

flag =True
while(1):
    if(s[ind] =="d"):
        if(s[ind + 5] =="d"):
            if(s[ind:ind+5]!=B):
                flag = False
                break
            else:
                ind+=5
        else:
            if(s[ind+5]!='e'):
                flag=False
                break
            if(s[ind+7] == "a"):
                if(s[ind:ind+5]!=B):
                    flag = False
                    break
                else:
                    ind+=5

            else:
                if(s[ind:ind+7] !=A):
                    flag = false
                    break
                else:
                    ind+=7
    elif(s[ind]=="e"):
        if(s[ind+5]=="r"):
            if(s[ind:ind+6]!=D):
                flag = False
                break
            else:
                ind+=6

        else:
            if(s[ind:ind +5] !=C):
                flag =False
                break
            else:
                ind+=5
    else:
        flag=False
        break
    if(ind == len_s):
        break
    if(ind > len(s)):
        flag=False
        break
if(flag):
    print("YES")
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
s=input()
ind =0
len_s=len(s)
s = s+ "zzzzzzzzzz"

A = "dreamer"
B= "dream"
C= "erase"
D="eraser"

flag =True
while(1):
    if(s[ind] =="d"):
        if(s[ind + 5] =="d"):
            if(s[ind:ind+5]!=B):
                flag = False
                break
            else:
                ind+=5
        else:
            if(s[ind+5]!='e'):
                flag=False
                break
            if(s[ind+7] == "a"):
                if(s[ind:ind+5]!=B):
                    flag = False
                    break
                else:
                    ind+=5

            else(s[ind:ind+7] !=A):
                flag = false
                break
            else:
                ind+=7
    elif(s[ind]=="e"):
        if(s[ind+5]=="r"):
            if(s[ind:ind+6]!=D):
                flag = False
                break
            else:
                ind+=6

        else:
            if(s[ind:ind +5] !=C):
                flag =False
                break
            else:
                ind+=5
    else:
        flag=False
        break
    if(ind == len_s):
        break
    if(ind > len(s)):
        flag=False
        break
if(flag):
    print("YES")
else:
    print("NO")