iT邦幫忙

0

簡易練習-1 溫度單位轉換

這是當初剛學時,練習寫的一個寫function,用來轉換溫度。用到了包括if else判斷式, while迴圈, def func, format(), float(), str(), input()的基礎概念。

#溫度轉換練習
def temp_trans():
    
    while True:
        whitch_temp = str(input('請輸入要轉換的溫度單位是°C(攝氏)or °F(華氏): '))
        if whitch_temp == "C":
            temp_C = float(input('請輸入要轉換的溫度:'))
            temp_F = temp_C * 9/5 + 32
            print("{num}°{unit}".format(num = temp_C, unit = whitch_temp),
                  "等於 {}°F".format(temp_F))
            return
        elif whitch_temp == "F":
            temp_F = float(input('請輸入要轉換的溫度:'))
            temp_C = (temp_F - 32)*5/9
            print("%s°%s" %(temp_F, whitch_temp), "等於 %s°C" %(temp_C))
            return
        else:
            print('請您輸入正確的溫度單位"C"或"F"')
            
temp_trans()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言