我无法完全理解Python3x中Type和Value错误之间的区别 .
当我尝试使用float('string')而不是TypeError时,为什么会得到ValueError?不应该给出一个TypeError因为我传递一个'str'类型的变量要转换成float?
In [169]: float('string')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-169-f894e176bff2> in <module>()
----> 1 float('string')
ValueError: could not convert string to float: 'string'
1 回答
值错误是
float
函数可以带一个字符串,即float('5')
,只是float('string')
中的值'string'
是一个不合适的(不可兑换的)字符串另一方面,
因此,如果您尝试使用
float(['5'])
,则会得到TypeError
因为列表永远不能转换为浮点数 .Cite