115103143 HW01 Python 環境建立和Hello World

1. https://notebook.google.com/notebook/4128868c-0396-4299-a2f5-373114934249



3.























4.def temp_converter():
    print("=== 溫度轉換器 ===")
    print("1. 攝氏 (℃) 轉 華氏 (℉)")
    print("2. 華氏 (℉) 轉 攝氏 (℃)")

    choice = input("請選擇轉換類型 (1 或 2): ").strip()

    if choice == "1":
        celsius = float(input("請輸入攝氏溫度 (℃): "))
        # 轉換公式:F = C * 9/5 + 32
        fahrenheit = (celsius * 9 / 5) + 32
        print(f"\n👉 {celsius:.1f}℃ = {fahrenheit:.1f}℉")

    elif choice == "2":
        fahrenheit = float(input("請輸入華氏溫度 (℉): "))
        # 轉換公式:C = (F - 32) * 5/9
        celsius = (fahrenheit - 32) * 5 / 9
        print(f"\n👉 {fahrenheit:.1f}℉ = {celsius:.1f}℃")

    else:
        print("輸入無效,請選擇 1 或 2。")


if __name__ == "__main__":
    temp_converter()

核心公式說明

  • 攝氏轉華氏:

  • 華氏轉攝氏:

程式特點

  1. 雙向轉換:讓使用者選要「C 轉 F」還是「F 轉 C」。

  2. 小數點控制:輸出使用 :.1f 將結果格式化,保留至小數點後第 1 位,畫面簡潔。





留言