저는 바이오 방정식 계산기를 작성 중이며 일부는 온도 변환기입니다.
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'
내가 여기서 무엇을했는지 잘 모르겠습니다.
이 input()
함수는 str
객체를 반환 합니다. float
수학 표현식에서 사용 하기 전에 문자열을로 변환해야합니다 . 그래서,
섭씨 :
T = (float(T_C) - 273.15)
화씨 :
T = ((float(T_F) + 459.67) * 5/9)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다