m0_68180072 2022-06-17 17:54 采纳率: 72.7%
浏览 38
已结题

请问,python 里面 , int()什么情况会转换失败?

img

也就是什么情况会ValueError?

凑字数:当蜘蛛网无情地尘封了我的烛台……

  • 写回答

2条回答 默认 最新

  • Hann Yang 全栈领域优质创作者 2022-06-17 18:12
    关注

    不能转的字串ValueError
    复数,列表,集合等 TypeError

    >>> int('1.2')
    Traceback (most recent call last):
      File "<pyshell#16>", line 1, in <module>
        int('1.2')
    ValueError: invalid literal for int() with base 10: '1.2'
    >>> int('a')
    Traceback (most recent call last):
      File "<pyshell#17>", line 1, in <module>
        int('a')
    ValueError: invalid literal for int() with base 10: 'a'
    >>> 
    >>> int(1+1j)
    Traceback (most recent call last):
      File "<pyshell#19>", line 1, in <module>
        int(1+1j)
    TypeError: can't convert complex to int
    >>> int([])
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        int([])
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
    >>> int({1})
    Traceback (most recent call last):
      File "<pyshell#21>", line 1, in <module>
        int({1})
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月26日
  • 已采纳回答 6月18日
  • 创建了问题 6月17日

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题