2014-09-24 72 views
0

這裏是我的代碼回溯(最近最後一次通話)的EOFError

t=input() 
for q in range(t): 
    s=raw_input() 
    m,n=s.split(' ') 
    ans = (m*n)*(m*n-1) 
    if(m>1 and n>1): 
    ans -= 4*(n-1)(m-2) + 4*(m-1)*(n-2) 
    print ans 

它錯誤

Traceback (most recent call last): 
    Line 1, in <module> 
    t=raw_input() 
EOFError 

我到底做錯了什麼?請告訴我 這裏的鏈接

http://codepad.org/nmL96e68

+0

鍵盤有標準輸入任何選項,試試http://ideone.com/z328vR。在你的系統上,運行文件或使用shell重定向後手動輸入這些項目:'python file.py 2014-09-24 11:11:58

+0

..並且你不能多個兩個字符串,所以首先將m和n轉換爲整數。 – 2014-09-24 11:14:03

+0

下一次使用* real * python解釋器而不是隨機在線服務。 – Bakuriu 2014-09-24 11:14:21

回答

0

這可能是你想要實現的,假設mn是整型值什麼,

t = input() 
for q in range(t): 
    s = raw_input() 
    m, n = map(int, s.split(' ')) 
    ans = (m * n) * (m * n - 1) 
    if m > 1 and n > 1: 
     ans -= 4 * (n - 1) * (m - 2) + 4 * (m - 1) * (n - 2) 
    print ans 
+0

謝謝,yaa是正確的,但爲什麼你使用地圖,我不想使用地圖,這裏是修改後的鏈接,你能告訴我現在我是如何克服這個錯誤的。 http://ideone.com/isvZP2 – Avneet 2014-09-24 11:26:16

+0

我會建議你使用'map'。修復縮進後,在代碼中選中第9行。將它從'ans - = 4 *(n-1)(m-2)+ 4 *(m-1)*(n-2)'改變爲'ans- = 4 *(n-1)*(m- 2)+ 4 *(m-1)*(n-2)' – 2014-09-24 11:29:12

+0

@Assians檢查此,http://ideone.com/KuTOrw – 2014-09-24 11:35:25

1

從Python文檔上input

相當於EVAL(的raw_input(提示))。

此功能不捕捉用戶錯誤。如果輸入不是語法有效的,則會引發SyntaxError。如果在評估過程中出現錯誤,可能會引發其他例外情況。

因此,如果您的輸入爲空,您可以獲得EOFError

避免使用第1行的輸入並改用raw_input。嘗試一些錯誤消息和驗證添加到您的代碼,像這樣:

import sys 
try: 
    t = int(raw_input()) 
except: 
    print "No repetition parameters set, using 1" 
    t = 1 
for q in range(t): 
    s = raw_input() 
    try: 
     m,n = s.split(' ') 
     m = int(m) 
     n = int(n) 
    except: 
     print "Invalid input, enter two integers separated by space" 
     sys.exit(1) 
    ans = (m*n)*(m*n-1) 
    if(m > 1 and n > 1): 
     ans -= 4*(n-1)*(m-2) + 4*(m-1)*(n-2) 
    print ans 

正如有人建議,始終測試在真實的Python解釋器代碼。

+0

try/except在這裏很重要,因爲如果用戶按下RETURN而沒有寫入任何輸入,則會引發EOFError。 – baxeico 2014-09-24 12:23:50

+0

感謝您的回覆哥們。對不起之前的評論被刪除。 – Avneet 2014-09-24 13:19:30

相關問題