Python “无法将‘float’对象隐式转换为str”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22397261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:52:51 来源:igfitidea点击:
"Can't convert 'float' object to str implicitly"
提问by Inferno
>>> 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"
采纳答案by Jayanth Koushik
floatscan't be implicitly converted into strings. You need to do it explicitly.
floats不能隐式转换为字符串。你需要明确地做到这一点。
print("The value from Fahrenheit to Celsius is " + str(celsius))
But it's better to use format.
但最好使用format.
print("The value from Fahrenheit to Celsius is {0}".format(celsius))
回答by jester
As the error says, you cannot covert float object to string implicitly. You have to do :
正如错误所说,您不能将浮动对象隐式转换为字符串。你必须要做 :
print("The value from Fahrenheit to Celsius is " + str(celsius))

