以 10 为基数的 int 的文字无效:''
>>> n = ''.join(i for i in x if i.isdigit())
>>> n.isdigit()
True
>>> x.isdigit()
False
>>> previous = 0
>>> next = 100
>>> answer = 0
>>> for i in range(0,100):
... answer += int(n[previous:next])
... previous = next
... next += 100
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: invalid literal for int() with base 10: ''
为什么我会收到此错误?正如你所看到的,n 是数字..
>>> n = ''.join(i for i in x if i.isdigit())
>>> n.isdigit()
True
>>> x.isdigit()
False
>>> previous = 0
>>> next = 100
>>> answer = 0
>>> for i in range(0,100):
... answer += int(n[previous:next])
... previous = next
... next += 100
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: invalid literal for int() with base 10: ''
Why am I getting this error ? As you can see n is digit..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
n
可能是数字,但在某些阶段您会超过n
的长度,使得n[previous:next]
不包含任何字符根本不。空字符串''
无法转换为 int,因此错误说明了全部情况:以 10 为基数的 int() 的文字无效:''
。n
might be numeric, but at some stage you're going past the length ofn
such thatn[previous:next]
contains no characters at all. The empty string''
cannot be converted to an int, hence the error which tells the full story:invalid literal for int() with base 10: ''
.