온도를 입력하면 다른 단위로 변환해 주는 파이썬 프로그램

이 포스트에서는 파이썬으로 온도를 입력하면 다른 단위로 변환해 주는 프로그램을 만들어 봅니다.

온도의 단위는 여러 단위가 있는데 그 중 가장 인지도 있는 단위는 섭씨, 화씨, 절대온도 이렇게 3가지입니다. 섭씨는 물의 삼중점을 0.01도(어는 점 0도)로 정의하고 끓는 점을 100도로 정의한, 우리나라(대한민국)을 포함한 세계 여러 나라에서 일상적으로 널리 쓰이고 있어 익숙한 단위입니다. 화씨는 미국에서 일상적으로 쓰고 있는 단위로, 물의 어는 점을 32도로 정의하고 끓는 점을 212도로 정의하여 화씨 100도는 사람의 체온과 비슷하게 됩니다. 절대온도는 일상적으로 잘 쓰이지 않고 주로 과학 분야에서 많이 쓰이는 단위로 물의 삼중점을 273.16으로 정의하고 이상 기체의 부피가 0이 되는 온도를 0으로 정의한 단위입니다.

우선, 섭씨를 화씨와 절대온도로 바꾸는 공식은 다음과 같습니다.

  • 섭씨 × 1.8 + 32 = 화씨, (화씨 – 32) ÷ 1.8 = 섭씨
  • 섭씨 + 273.15 = 절대온도, 절대온도 – 273.15 = 섭씨

화씨↔절대온도 변환은 섭씨의 변환공식을 거치면 되기 때문에 따로 서술하지 않았습니다.

이를 코딩해 보면 다음과 같습니다.

class temp_unit:
    celsius = 1
    fahrenheit = 2
    kelvin = 3

def temp_convert(value, source_unit, dest_unit):
    if source_unit == dest_unit:
        return value
    else:
        if source_unit == temp_unit.celsius:
            if dest_unit == temp_unit.fahrenheit:
                return value * 1.8 + 32
            elif dest_unit == temp_unit.kelvin:
                return value + 273.15
        elif source_unit == temp_unit.fahrenheit:
            if dest_unit == temp_unit.celsius:
                return (value - 32) / 1.8
            elif dest_unit == temp_unit.kelvin:
                return (value - 32) / 1.8 + 273.15
        elif source_unit == temp_unit.kelvin:
            if dest_unit == temp_unit.celsius:
                return value - 273.15
            elif dest_unit == temp_unit.fahrenheit:
                return (value - 273.15) * 1.8 + 32
    return None

if __name__ == '__main__':
    import re
    
    tmp_input = input("Input temperature: ")
    tmp_m = re.search(r'^\-?[0-9]+(\.[0-9]{1,2})?$', tmp_input )

    if tmp_m:
        tmp_value = float( tmp_m.group(0) )
        
        # Celsius to Fahrenheit and Kelvin
        print("%g C = %g F = %g K" % (
            tmp_value,
            temp_convert(tmp_value, temp_unit.celsius, temp_unit.fahrenheit),
            temp_convert(tmp_value, temp_unit.celsius, temp_unit.kelvin),
        ) )
        # Fahrenheit to Celsius and Kelvin
        print("%g F = %g C = %g K" % (
            tmp_value,
            temp_convert(tmp_value, temp_unit.fahrenheit, temp_unit.celsius),
            temp_convert(tmp_value, temp_unit.fahrenheit, temp_unit.kelvin),
        ) )
        # Kelvin to Celsius and Fahrenheit
        print("%g K = %g C = %g F" % (
            tmp_value,
            temp_convert(tmp_value, temp_unit.kelvin, temp_unit.celsius),
            temp_convert(tmp_value, temp_unit.kelvin, temp_unit.fahrenheit),
        ) )
        
    else:
        print("Invalid input.")

모듈형으로 코딩하였습니다. 1번 줄부터 4번 줄까지는 온도 단위 플래그를 정의할 클래스 상수, 6번 줄부터 25번 줄까지는 온도 변환을 위한 함수, 27번 줄부터는 import하지 않고 직접 실행시 실행될 부분입니다.

이를 실행해 보면 다음과 같습니다.

Input temperature: 23        
23 C = 73.4 F = 296.15 K     
23 F = -5 C = 268.15 K       
23 K = -250.15 C = -418.27 F 

23도를 변환한 예제입니다. 먼저 섭씨 23도는 화씨 73.4도와 절대온도 296.15로, 화씨 23도는 섭씨 -5도와 절대온도 268.15로, 절대온도 23은 섭씨 -250.15도와 화씨 -418.27도로 변환됩니다.

답글 남기기

이메일 주소는 공개되지 않습니다.