2016-02-27 51 views
1
n = input() 
dum = input() 
d = {} 
for i in range(0,n+1): 
    x = raw_input() 
    x = x.split(" ") 
    d[int(x[0])] = int(x[1]) 

array = d.keys() 

for key in d.keys(): 
    if(d[key]!=0): 
     if(d[key] not in d.keys()): 
      for i in d.keys(): 
       for j in d.keys(): 
        if(i!=j and i!=key and j!=key): 
         if(i+j==d[key]): 
          # print str(i)+"-"+str(j) 
          if(i in array): 

           array.remove(i) 
          if(j in array): 
           # print j 
           array.remove(j) 
     else: 
      # print d[key] 
      array.remove(d[key]) 
print array[0] 

當我執行這個Python代碼時,我在閱讀輸入時出現「EOF錯誤」。閱讀輸入時出現Python EOF錯誤

你能幫忙嗎?我正在運行的Python 2.7.5

錯誤回溯

Traceback (most recent call last): 
File "prog.py", line 1, in <module> 
EOFError: EOF when reading a line 
+0

我收到IndexError:列表索引超出範圍 – AlokThakur

+0

請包含完整的錯誤回溯。 – Forge

+0

如果數組爲空,您將獲得該數組。但是由於EOF錯誤,我無法執行代碼。 – Mark

回答

1

我似乎無法雖然使用相同的輸入像你一樣重現此錯誤。也許你在輸入之前有一個newline字符?
嘗試在您的終端中使用python prog.py運行此代碼。

EOF error如果在撥打inputraw_input時沒有給出數據,則應如documentation中所述。

此外,當從用戶輸入python 2時,建議使用raw_input而不是input,但它不會修復您的錯誤。

0

在Python 2中,raw_input()返回一個字符串,並且input()嘗試將輸入作爲Python表達式運行。所以,把你的第一行改成這樣的東西應該可行。

n = int(raw_input()) 

根據官方documentation

Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

0

此:

for i in range(0,n+1): 

確實n+1迭代次數,但輸入文件:

5 
6 
11 21 
21 0 
31 52 
41 61 
61 0 

只有n該循環即將開始時剩餘的行數。在嘗試讀取第n+1行時,您將收到EOFError,因爲沒有更多行。