1 出现原因
- 使用了
int()
函数 - 且
int()
转化的对象为非数字型字符串
或者浮点型数字字符串
2 出现的情景
- 数字型字符串无法转化
int('1.5')
Traceback (most recent call last):
File "\testPY.py", line 1, in <module>
int('1.5')
ValueError: invalid literal for int() with base 10: '1.5'
- 非法字符报错
int('aaa')
Traceback (most recent call last):
File "\testPY.py", line 1, in <module>
int('aaa')
ValueError: invalid literal for int() with base 10: 'aaa'
3 解决字符串类型的数据转化
print(eval("1.5"))
1.5