Python 3:TypeError:x2B;的操作数类型不受支持:';浮动';和';str';

Python 3:TypeError:x2B;的操作数类型不受支持:';浮动';和';str';,python,python-3.x,Python,Python 3.x,有人能告诉我为什么我会得到错误代码:TypeError:不支持的+操作数类型:'float'和'str' while True: c = input("Gib die Temperatur in Grad Celcius ein: ") try: c = float(c) return c except ValueError: print(&quo

有人能告诉我为什么我会得到错误代码:TypeError:不支持的+操作数类型:'float'和'str'

    while True:
        c = input("Gib die Temperatur in Grad Celcius ein:  ")
        try:
            c = float(c)
            return c
        except ValueError:
            print("Das ist keine gültige Angabe für eine Temperatur")


def convert_to_temperature(c):
    k = c + 273.15
    return k


if __name__ == "__main__":
    c = get_temperature()
    print("Das sind " + str(convert_to_temperature(c) + " Kelvin."))

当您试图将
convert\u转换为\u temperature(c)
的结果时,括号放错了位置,该结果将浮点值转换为字符串。这是不正确的行,因为
str()
正在应用于
convert\u to\u temperature(c)+“Kelvin.”
,如果您检查各个类型:

print(type(convert_to_temperature(c)))
print(type(" Kelvin."))
返回:

float
str
因此,
str()
必须仅应用于
convert\u to\u temperature(c)
的输出:


允许通过
+
运算符连接字符串。

我没有看到
获取温度()
函数。
打印(“Das sind”+str(convert_to_temperature(c))+“Kelvin.”)
或更简单的,
打印(“Das sind”,convert_to_temperature(c),“Kelvin.”)
检查支架的位置,最后一行应该是
print(“Das sind”+str(convert_to_temperature(c))+“Kelvin.”)
-或者作为f字符串
print(f“Das sind{convert_to_temperature(c)}Kelvin.”)
与错误无关,但我建议为
convert_to_temperature
取一个更好的名称。由于该参数已经是以摄氏度为单位的温度,您可能需要调用此函数
将摄氏度转换为华氏度
I think you missed the get_temperature() function header (supplied here). And the last line you can modify to make it work: 

def get_temperature():
        while True:
            c = input("Gib die Temperatur in Grad Celcius ein:  ")
            try:
                c = float(c)
                return c
        
            except ValueError:
                print("Das ist keine gültige Angabe für eine Temperatur")
    
    
    
    
    if __name__ == "__main__":
        c = get_temperature()
        print(f"Das sind {convert_to_temperature(c)} Kelvin.")
I think you missed the get_temperature() function header (supplied here). And the last line you can modify to make it work: 

def get_temperature():
        while True:
            c = input("Gib die Temperatur in Grad Celcius ein:  ")
            try:
                c = float(c)
                return c
        
            except ValueError:
                print("Das ist keine gültige Angabe für eine Temperatur")
    
    
    
    
    if __name__ == "__main__":
        c = get_temperature()
        print(f"Das sind {convert_to_temperature(c)} Kelvin.")