This question already has answers here:
TypeError: Can't convert 'int' object to str implicitly

(2个答案)


7年前关闭。



>>> 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"

最佳答案

floats不能隐式转换为字符串。您需要明确地做到这一点。

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

但是最好使用format
print("The value from Fahrenheit to Celsius is {0}".format(celsius))

关于python - "Can' t将'float'对象隐式转换为str”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22397261/

10-12 16:21