Skip to main content
 首页 » 编程设计

Python 3 错误 : ValueError: invalid literal for int() with base 10: ''

2025年05月04日31yxwkf

我已经编写了解决问题的代码。

这是我的代码:

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 上运行它或将其作为解决方案提交时,它在 第 5 行 上给出了一个错误:

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

我不确定是什么问题。

请您参考如下方法:

如果用户没有输入任何内容,换句话说就是空字符串,则您不会处理

>>> 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更稳健地接受来自用户的输入。