2016-02-25 76 views
1

我已經編寫了解決問題的代碼。Python 3錯誤:ValueError:int()與基數爲10的無效文字:''

這裏是我的代碼:

t=int(input()) 
print("") 
while(t): 
    s=0 
    n=int(input()) 
    for i in range(n): 
     no=int(input()) 
     s=s+no 
    if s%n==0: 
     print("YES") 
    else: 
     print("NO") 
    print("") 
    t-=1 

當我在OS X終端上運行它,它工作正常。但是,當我在IDEone運行,或將其提交的解決方案,它提供了有關line 5錯誤:

ValueError: invalid literal for int() with base 10: ''

我不確定的是什麼問題。

+0

那麼也許你應該在嘗試將'input'轉換爲'int'之前檢查'input'的結果。 –

回答

5

你不處理,如果用戶輸入什麼,換句話說空字符串

>>> int('') 
Traceback (most recent call last): 
    File "<pyshell#3>", line 1, in <module> 
    int('') 
ValueError: invalid literal for int() with base 10: '' 

您可以使用a variety of techniques從用戶更有力地接受輸入。

相關問題