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

请问,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日