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

Mar*_*jus 3 python casting slice

>>> 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: ''
Run Code Online (Sandbox Code Playgroud)

为什么我收到此错误?如你所见,n是数字..

mar*_*cog 7

n可能是数字,但在某个阶段你路过的长度n,使得n[previous:next]包含任何字符.空字符串''不能转换为int,因此错误告诉完整的故事: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: ''
Run Code Online (Sandbox Code Playgroud)