4.3 类型转换错误

当然,如果向 int()float() 提供的不是一个数,它就会不正常。

下面来试试看:

  1. >>> print float('fred')
  2. Traceback (most recent call last):
  3. File "<pyshell#1>", line 1, in <module>
  4. print float ('fred')
  5. ValueError: could not convert string to float: fred

我们得到了一个错误消息。这个非法文字(invalid literal)错误消息说明 Python 不知道怎么从 "fred" 创建一个数。如果是你,你知道吗?