加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 基数为10的int的文字无效:”

发布时间:2020-12-20 11:16:19 所属栏目:Python 来源:网络整理
导读: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):
>>> 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可能是数字,但在某个阶段你会超过n的长度,使得n [previous:next]根本不包含任何字符.空字符串”不能转换为int,因此错误告诉完整的故事:int(无效的基数为10的文字:”.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读