Invalid literal for int with base 10: ''

>>> 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 a digit.

+3
source share
1 answer

nmay be numeric, but at some point you go past the length n, so it n[previous:next]doesn't contain characters at all. Empty string ''can not be converted to int, therefore, an error that 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: ''
+7
source

Source: https://habr.com/ru/post/1781135/


All Articles