我正在为aa生物等式编写计算器,并且它的一部分是一个温度转换器。TypeError:不能将'float'对象隐式转换为str或TypeError:不支持的操作数类型为 - :'str'和'float'
T_option = input("Celsius, fahrenheit, or Kelvin? ").lower()
if T_option == "celsius" or T_option == "c":
T_C = input("Input temperature: ")
T = (T_C - 273.15)
print("Temp set to " + str(T) + "K")
elif T_option == "kelvin" or T_option == "k":
T = input("Input temperature: ")
print("Temp set to " + str(T) + "K")
elif T_option == "fahrenheit" or T_option == "f":
T_F = input("Input temperature: ")
T = ((T_F + 459.67) * 5/9)
("Temp set to " + str(T) + "K")
如果让我选择华氏度,输入一个数字,我得到:
TypeError: Can't convert 'float' object to str implicitly
如果我选择摄氏度,然后输入一个号码,我得到:
TypeError: unsupported operand type(s) for -: 'str' and 'float'
我不知道我在这里做了什么。
如果您正在运行python-2.7,则必须防止整数除法,例如,将5/9变成5.0/9.0。 –