python - "Can' t 将 'float' 对象隐式转换为 str"

标签 python typeerror

<分区>

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

但最好使用格式

print("The value from Fahrenheit to Celsius is {0}".format(celsius))

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

相关文章:

python - 在 matplotlib 条形图中显示非常窄的矩形的颜色

python - Pandas 切割方法排除下限

python - 错误: TypeError: circle() takes no keyword arguments

python - 类型错误:+ 不支持的操作数类型: 'decimal' 和 'float'

python - 使用字典作为链接更改对象

python - Flask Web 服务器的多处理与多线程

python - 调用在 Python 中如何工作?

python - 是否存在无法创建深拷贝的对象?

python - 如何使用Python解析Selenium : TypeError: 'module' object is not callable

python - 调用 scipy griddata 时出现类型错误