Problem p02664 (python)

Fixed submission:
T = list(input())
N = len(T)
if T[-1] == "?":
    T[-1] = "D"

if N == 1 and T[0] == "?":
    T[0] = D
elif N == 2:
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
                
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
                
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
        
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
        
elif N > 2:        
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
          
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
            
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "D":
        T[1] = "P"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "P":
        T[1] = "D"
    
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
        
    for i in range(2,N-1):
        if T[i-2] == "P" and T[i-1] == "D" and T[i+1] == "D" and T[i] == "?":
            T[i] = "P"
            continue
        
        elif T[i] == "?":
            T[i] = "D"
        

ans = "".join(T)         
print(ans)

Buggy submission (verdict: Runtime Error):
T = list(input())
N = len(T)
if T[-1] == "?":
    T[-1] = "D"

if N == 1 and T[0] == "?":
    T[0] = D
elif N == 2:
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
                
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
                
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
        
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
elif N > 2:        
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
          
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
            
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "D":
        T[1] = "P"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "P":
        T[1] = "D"
    
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
        
    for i in range(2,N-1):
         if T[i-2] == "P" and T[i-1] == "D" and T[i+1] == "D" and T[i] == "?":
            T[i] = "P"
              continue
        elif T[i] == "?":
            T[i] = "D"
        

ans = "".join(T)         
print(ans)

Buggy submission (verdict: Runtime Error):
T = list(input())
N = len(T)
if T[-1] == "?":
    T[-1] = "D"

if N == 1 and T[0] == "?":
    T[0] = D
elif N == 2:
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
                
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
                
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
        
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
elif N > 2:        
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
          
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
            
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "D":
        T[1] = "P"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "P":
        T[1] = "D"
    
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
        
    for i in range(2,N-1):
         if T[i-2] == "P" and T[i-1] == "D" and T[i+1] == "D" and T[i] == "?":
             T[i] = "P"
              continue
        elif T[i] == "?":
            T[i] = "D"
        

ans = "".join(T)         
print(ans)

Buggy submission (verdict: Runtime Error):
T = list(input())
N = len(T)

if N == 1 and T[0] == "?":
    T[0] = D
elif N == 2:
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
                
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
                
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
        
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
elif N > 2:        
    if T[0] == "?" and T[1] == "?":
        T[0] = "P"
        T[1] = "D"
          
    elif T[0] == "?" and T[1] == "D":
        T[0] = "P"
            
    elif T[0] == "?" and T[1] == "P":
        T[0] = "D"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "D":
        T[1] = "P"
            
    elif T[0] == "D" and T[1] == "?" and T[2] == "P":
        T[1] = "D"
    
    elif T[0] == "P" and T[1] == "?":
        T[1] = "D"
        
if T[-1] == "?":
    T[-1] = "D"
    


for i in range(2,N-1):
    if T[i-2] == "P" and T[i-1] == "D" and T[i+1] == "D" and T[i] == "?":
        
        T[i] = "P"
        
        continue
    elif T[i] == "?":
       
        T[i] = "D"
        

ans = "".join(T)         
print(ans)

Buggy submission (verdict: Runtime Error):
T = input()
N = len(T)

if T[0] == "?" and T[1] == "?":
    T2 = list(T)
    T2[0] = "P"
    T2[1] = "D"
    T = "".join(T2)
        
elif T[0] == "?" and T[1] == "D":
    T2 = list(T)
    T2[0] = "P"
    T = "".join(T2)
        
elif T[0] == "?" and T[1] == "P":
    T2 = list(T)
    T2[0] = "D"
    T = "".join(T2)
        
elif T[0] == "D" and T[1] == "?" and T[2] == "D":
    T2 = list(T)
    T2[1] = "P"
    T = "".join(T2)
        
elif T[0] == "D" and T[1] == "?" and T[2] == "P":
    T2 = list(T)
    T2[1] = "D"
    T = "".join(T2)
elif T[0] == "P" and T[1] == "?":
    T2 = list(T)
    T2[1] = "D"
    T = "".join(T2)

if T[-1] == "?":
    T2 = list(T)
    T2[-1] = "D"
    T = "".join(T2)


for i in range(2,N-1):
    if T[i-2] == "P" and T[i-1] == "D" and T[i+1] == "D" and T[i] == "?":
        T2 = list(T)
        T2[i] = "P"
        T = "".join(T2) 
        continue
    elif T[i] == "?":
        T2 = list(T)
        T2[i] = "D"
        T = "".join(T2) 

        
print(T)