Codechef上的NZEC错误

时间:2017-07-10 07:18:48

标签: python

我是codechef的新手,它为我的下面的代码提供了这个NZEC错误。我已经彻底搜索过但无法找到解决问题的方法。

https://www.codechef.com/problems/SNELECT

t= int(raw_input()) #accepts input
i = 1    
while i <= t:    
    input_str = raw_input() #accepts the input for further working
    elec_list = []    #empty list to store individual string elements
    for animal in input_str:    #reiterating over each element of string
        elec_list.append(animal)    #and adding to our list
#now we will go over elec_list and make mongoose eat at most one of their adjacent snakes.
    for index, animal in enumerate(elec_list):
        if animal == 'm':
            if index and index != len(elec_list):
                if elec_list[index - 1] == 's':
                    elec_list[index - 1] = ''
                elif elec_list[index + 1] == 's':
                    elec_list[index + 1] = ''
            elif not index:
                if elec_list[index + 1] == 's':
                    elec_list[index + 1] = ''
            elif index == len(elec_list):
                if elec_list[index - 1] == 's':
                    elec_list[index - 1] == ''
    #now counting the number of snakes and mongooses.
    mon_vote = elec_list.count('m')
    sna_vote = elec_list.count('s')
    #printing the output
    if sna_vote > mon_vote:
        print "snakes"
    elif mon_vote > sna_vote:
        print "mongooses"
    else:
        print "tie"

2 个答案:

答案 0 :(得分:0)

首先让我告诉你你得到了什么错误。这是您执行的代码 Your code 你遇到的问题是

EOFError: EOF when reading a line

这种情况正在发生,因为您的循环中没有递增i,因为无限循环条件正在发生。

您只需在循环结束后将i递增1,您的问题就会得到解决。希望它有所帮助。

修改

此后可能遇到的另一个问题是由于代码中的拼写错误。我想

elec_list[index - 1] == ''应更改为elec_list[index - 1] = ''

答案 1 :(得分:0)

将所有代码放入Try and Except因为Codechef,使用文件作为输入。我在代码中做了同样的事情,并且可以正常工作。

    try:    
        t= int(raw_input()) #accepts input
        i = 1    
        while i <= t:    
            input_str = raw_input() #accepts the input for further working
            elec_list = []    #empty list to store individual string elements
            for animal in input_str:    #reiterating over each element of string
                elec_list.append(animal)    #and adding to our list
        #now we will go over elec_list and make mongoose eat at most one of their adjacent snakes.
            for index, animal in enumerate(elec_list):
                if animal == 'm':
                    if index and index != len(elec_list):
                        if elec_list[index - 1] == 's':
                            elec_list[index - 1] = ''
                        elif elec_list[index + 1] == 's':
                            elec_list[index + 1] = ''
                    elif not index:
                        if elec_list[index + 1] == 's':
                            elec_list[index + 1] = ''
                    elif index == len(elec_list):
                        if elec_list[index - 1] == 's':
                            elec_list[index - 1] == ''
            #now counting the number of snakes and mongooses.
            mon_vote = elec_list.count('m')
            sna_vote = elec_list.count('s')
            #printing the output
            if sna_vote > mon_vote:
                print("snakes")
            elif mon_vote > sna_vote:
                print("mongooses")
            else:
                print("tie")
    except:
        pass