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日

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)