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 "", line 1, in
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)

>>> int(a)
10

转载于:https://www.cnblogs.com/ddpeng/p/9759145.html

你可能感兴趣的:(ValueError: invalid literal for int() with base 10: '10.1')