【问题标题】:Python 3 error: ValueError: invalid literal for int() with base 10: ''Python 3 错误:ValueError:int() 的无效文字,基数为 10:''
【发布时间】:2016-02-25 23:06:20
【问题描述】:

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

这是我的代码:

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: int() 以 10 为底的无效文字:''

我不确定问题是什么。

【问题讨论】:

  • 那么也许您应该在尝试将其转换为 int 之前检查来自 input 的结果。

标签: python python-3.x


【解决方案1】:

如果用户没有输入任何内容,也就是空字符串,则您没有处理

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2016-11-14
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多