"无法转换'浮动'反对str含义"

时间:2014-03-14 05:55:54

标签: python typeerror

>>> def main():
        fahrenheit = eval(input("Enter the value for F: "))
        celsius = fahrenheit - 32 * 5/9
        print("The value from Fahrenheit to Celsius is " + celsius)
>>> main()
Enter the value for F: 32
Traceback (most recent call last):  
  File "<pyshell#73>", line 1, in <module>
    main()
  File "<pyshell#72>", line 4, in main
    print("The value from Fahrenheit to Celsius is " + celsius)
TypeError: Can't convert 'float' object to str implicitly"

2 个答案:

答案 0 :(得分:16)

floats无法隐式转换为字符串。你需要明确地做到这一点。

print("The value from Fahrenheit to Celsius is " + str(celsius))

但最好使用format

print("The value from Fahrenheit to Celsius is {0}".format(celsius))

答案 1 :(得分:-1)

如错误所示,您无法隐式地将float对象转换为字符串。你必须这样做:

print("The value from Fahrenheit to Celsius is " + str(celsius))