以 10 为基数的 int 的文字无效:''

发布于 2024-10-08 05:17:44 字数 516 浏览 0 评论 0原文

>>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-10-15 05:17:44

n 可能是数字,但在某些阶段您会超过 n 的长度,使得 n[previous:next] 不包含任何字符根本不。空字符串 '' 无法转换为 int,因此错误说明了全部情况:以 10 为基数的 int() 的文字无效:''

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

n might be numeric, but at some stage you're going past the length of n such that n[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: ''.

>>> int('')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文