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

时间:2022-12-27 22:33:01

自定义输入时,提示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