NZEC Runtime Error in Python 2.7 in SPOJ
Question: I think my algo is right(may be done very badly) But I get the desired outputs
in ideone.com. But in SPOJ it keeps on saying "Runtime Error NZEC". Please
suggest a few changes to get this right.

Here is the link to the Question :

<http://www.spoj.com/problems/RAFANOLE/>

Here is my code :

    
    
    import sys;
    inp=sys.stdin.read().split("\n");
    t=int(inp[0]);
    i=1;
    g_N=dict();
    g_D=dict();
    t_N=dict();
    t_D=dict();
    while(i<t+2):
        a=inp[i].split();
        game_N =0;
        curr_N =0;
        game_D =0;
        curr_D =0;
        tie_N=0;
        tie_D=0;
        k=0;
        n=len(a);
        while ((k<n)and((game_N<6)or(game_D<6))):
            if(a[k]=='N'):
                curr_N = curr_N +1;
                if(curr_N==4 and curr_D < 3):
                    game_N=game_N +1;
                    curr_N=0;
                    curr_D=0;
                if(curr_N==3 and curr_D ==3) :
                    while(a[k]!=a[k+1]):
                        k=k+1;
                    if(a[k]=='N'):
                        game_N=game_N+1;
                        curr_N=0;
                        curr_D=0;
            if(a[k]=='D'):
                curr_D=curr_D+1;
                if(curr_D==4 and curr_N<3):
                  game_D=game_D+1;
                  curr_D=0;
                  curr_N=0;
                if(curr_N==3 and curr_D ==3) :
                   while(a[k]!=a[k+1]):
                       k=k+1;
                   if(a[k]=='D'):
                       game_D=game_D+1;
                       curr_N=0;
                       curr_D=0;
            k=k+1;
            if (game_N==6 and game_D ==6):
                break;
        if((game_N==6 and game_D==5 ) or (game_N==5 and game_D==6)):
            curr_N=0
            while(game_N != 7 and game_D != 7):
                if (a[k]=='N'):
                    curr_N=curr_N+1;
                if(a[k]=='D'):
                    curr_D=curr_D+1;
                if(curr_N==4 and curr_D<3):
                    game_N=game_N+1;
                if(curr_D==4 and curr_N<3):
                    game_D=game_D+1;
        if (game_N==6 and game_D ==6):
            while (1):
                if(a[k]=='N'):
                    tie_N=tie_N+1;
                if(a[k]=='D'):
                    tie_D=tie_D+1;
                if(((tie_D==7 or tie_N==7) and (abs(tie_D-tie_N)>=2)) or ((tie_D>7 or tie_N>7)and(abs(tie_D-tie_N)>=2))):
                    break;
                k=k+1;
    
        if(tie_N>tie_D):
            game_N=game_N+1;
        elif(tie_N<tie_D) :
            game_D=game_D+1;
        g_N[i]=game_N;
        g_D[i]=game_D;
        t_N[i]=tie_N;
        t_D[i]=tie_D;
    
        i=i+2;
    i=1;
    while(i<t+2):        
        if(g_N[i]>g_D[i]):
            if(t_N[i]==0 and t_D[i] ==0):
                print ("N %d" % g_N[i]);
                print ("D %d" % g_D[i]);
                print "\t";
            else :
                print ("N %d(%d-%d)"%(g_N[i],t_N[i],t_D[i]));
                print ("D %d" % g_D[i]);
        else:
            if(t_N[i]==0 and t_D[i] ==0):
                print ("D %d" % g_D[i]);
                print ("N %d" % g_N[i]);
            else :
                print ("D %d(%d-%d)"%(g_D[i],t_D[i],t_N[i]));
                print ("N %d" % g_N[i]);
        i=i+2;
    

Thanks in Advance.


Answer: You are not considering the fact that the input data ends. `sys.stdin` is a
file like object and requires an EOF character to mark the end. As it is not
present, python tries to read more data when none is present and you get an
error.

You should use `input` or `raw_input` instead of `sys.stdin`. As an example,
in the code above, replace the 2nd and the 3rd lines with (assuming you use
Python 2.7)

    
    
    t = input("")
    

Also replace the first line in the first `while` with

    
    
    a = input("")
    

