“Can't convert 'float' object to str implicitly”
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 | >>> 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" |
无法将
1 | print("The value from Fahrenheit to Celsius is" + str(celsius)) |
但最好使用
1 | print("The value from Fahrenheit to Celsius is {0}".format(celsius)) |
正如错误所说,不能将float对象隐式转换为字符串。你必须这样做:
1 | print("The value from Fahrenheit to Celsius is" + str(celsius)) |