python中的int函数可以将数字或字符串转换为整型数字类型,具体功能就不提了

最近发现一个问题,对于字符串'1.1'之类的,int转换的时候会报异常,这是为什么,个人感觉直接转换成1不就行了,干嘛还不能转换了

>>> int('1.1')

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

这是坑,还是故意的,有没有大神解释一下这是为什么要这么设定

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2021-11-23
  • 2021-06-02
猜你喜欢
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案