Python &引用;Can';t转换为';浮动';隐式地反对str”; >>def main(): 华氏温度=蒸发(输入(“输入F:”)的值) 摄氏=华氏-32*5/9 打印(“从华氏到摄氏的值为”+摄氏度) >>>main() 输入F:32的值 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 main() 文件“”,第4行,主 打印(“从华氏到摄氏的值为”+摄氏度) TypeError:无法将“float”对象隐式转换为str“
正如错误所述,您无法将float对象隐式转换为string。您必须执行以下操作:Python &引用;Can';t转换为';浮动';隐式地反对str”; >>def main(): 华氏温度=蒸发(输入(“输入F:”)的值) 摄氏=华氏-32*5/9 打印(“从华氏到摄氏的值为”+摄氏度) >>>main() 输入F:32的值 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 main() 文件“”,第4行,主 打印(“从华氏到摄氏的值为”+摄氏度) TypeError:无法将“float”对象隐式转换为str“,python,typeerror,Python,Typeerror,正如错误所述,您无法将float对象隐式转换为string。您必须执行以下操作: >>> def main(): fahrenheit = eval(input("Enter the value for F: ")) celsius = fahrenheit - 32 * 5/9 print("The value from Fahrenheit to Celsius is " + celsius) >>> mai
>>> 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"
浮动
不能隐式转换为字符串。您需要显式转换
print("The value from Fahrenheit to Celsius is " + str(celsius))
但最好使用格式
print("The value from Fahrenheit to Celsius is " + str(celsius))
如果您使用的是Python2.7+,则可以省略
str.format
中的位置参数说明符,因此{0}
相当于{}
print("The value from Fahrenheit to Celsius is {0}".format(celsius))