本文介绍了“无法将 'float' 对象隐式转换为 str"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
>>>定义主():fahrenheit = eval(input("请输入 F 的值:"))摄氏度 = 华氏度 - 32 * 5/9print("华氏到摄氏的值是" + 摄氏度)>>>主要的()输入 F 的值:32回溯(最近一次调用最后一次):文件<pyshell#73>",第 1 行,在 <module> 中主要的()文件<pyshell#72>",第 4 行,在 mainprint("华氏到摄氏的值是" + 摄氏度)类型错误:无法将 'float' 对象隐式转换为 str" 解决方案
floats
不能隐式转换为字符串.你需要明确地去做.
print("华氏到摄氏的值是" + str(celsius))
但最好使用format
.
print("华氏到摄氏的值是{0}".format(celsius))
>>> 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
can't be implicitly converted into strings. You need to do it explicitly.
print("The value from Fahrenheit to Celsius is " + str(celsius))
But it's better to use format
.
print("The value from Fahrenheit to Celsius is {0}".format(celsius))
这篇关于“无法将 'float' 对象隐式转换为 str"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!