我正在爲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。 –