ValueError: invalid literal for int() with base 10: '10.1'

自定义输入时,提示ValueError: invalid literal for int() with base 10: '10.1'错误

>>> a = int(raw_input('please input something:'))
please input something:10.1


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

 

原因是数字字符串不能直接转int类型,需要转为float类型后才能转int类型:

>>> int(float("10.1"))
10

或者:

>>> a = float(raw_input('please input something:'))
please input something:10.1
>>> type(a)
<type 'float'>
>>> int(a)
10

posted on 2018-10-09 11:02  蜗牛前进的方向  阅读(17475)  评论(0编辑  收藏  举报

导航