前置き
日本では、気温を測るときに、摂氏(°C)が使用されているが、アメリカでは華氏(°F)が使用されている。ほとんどの国においては、摂氏(°C)が使用されている。今日は、
摂氏(°C)から華氏(°F)に変換するプログラムコードを作成してみたかった。
プログラムコード (気温を摂氏(°C)から華氏(°F)に変換)
import tkinter as tk
from tkinter import ttk
def celcius_to_fahrenheit():
celcius_to_fahrenheit = float(celcius_input.get())
celcius = celcius_to_fahrenheit * 9/5 + 32
celcius_result_label.config(text=f"{celcius}")
window = tk.Tk()
window.title("ºC to ºF converter")
window.config(padx = 20, pady = 20)
# Get screen width and height
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
# Calculate window width and height
window_width = int(screen_width * 0.8)
window_height = int(screen_height * 0.8)
# Set window size
window.wm_geometry(f"{window_width}x{window_height}")
celcius_input = tk.Entry(width = 7)
celcius_input.grid(column = 1, row = 0)
celcius_label = tk.Label(text = "celcius")
celcius_label.grid(column = 2, row = 0)
is_equal_label = tk.Label(text = "is equal to")
is_equal_label.grid(column = 0, row = 1)
celcius_result_label = tk.Label(text = "0")
celcius_result_label.grid(column = 1, row = 1)
fahrenheit_label = tk.Label(text = "°F")
fahrenheit_label.grid(column = 2, row = 1)
calculate_button = tk.Button(text = "Calculate", command = celcius_to_fahrenheit)
calculate_button.grid(column = 1, row = 2)
window.mainloop()
プログラムコードの説明
ここでは、celcius_to_fahrenheitという関数を定義している。celcius(摂氏の気温)ユーザーが数字を入力したら、fahrenheit(華氏の気温)が出力される。
日本で、1番の最高気温を観測したのは、気象庁の前進である中央観測台が1923年9月2日の未明に、麹町区元衛町(現在の竹橋付近)にて観測した46.3度だ。そこで、私は46.3度が華氏何度になるのか興味を持って、このプログラムコードを使って出力してみた。
引用元:https://news.yahoo.co.jp/expert/articles/45ea611b16f491cd2383a4deaab2675140dc2309
最近で、1番の最高気温を観測したのは、41.1度で、静岡県浜松市の2020年8月17日と、
埼玉県熊谷市の2018年7月23日となっている。
引用元:https://www.pref.shizuoka.jp/kensei/information/myshizuoka/1002261/1040947/1011346.html#:~:text=%E6%B0%97%E8%B1%A1%E5%BA%81%E3%80%8C%E6%AD%B4%E4%BB%A3%E5%85%A8%E5%9B%BD%E3%83%A9%E3%83%B3%E3%82%AD%E3%83%B3%E3%82%B0%E3%80%8D%E3%81%AB%E3%82%88%E3%82%8B,%E6%9A%91%E3%81%95%E3%81%A8%E3%81%AA%E3%82%8A%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82
出力結果
46.3度は、華氏では、115.34度になる。
摂氏から華氏を求めるには、(摂氏 x 9/5) + 32 の計算式が用いられている。
46.3 x 9/5 = 46.3 x 1.8 = 83.34
83.34 + 32 = 115.34となり、計算結果が一致している。
ここで、私は疑問に思った。摂氏から華氏を求めるだけでは面白くないだろう。
摂氏(°C)と華氏(°F)を相互変換するプログラムコードの作成に踏み切った。
ちなみに、100°Fは、37.8°Cになる。
プログラムコード(気温を摂氏(°)から華氏(°F)に相互変換)
def celsius_to_fahrenheit(celsius):
"""
摂氏から華氏への変換
"""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
"""
華氏から摂氏への変換
"""
celsius = (fahrenheit - 32) * 5/9
return celsius
def convert_temperature(temp_str):
"""
温度と単位が入力された文字列を受け取り、変換された温度と単位を表示する
"""
if 'c' in temp_str.lower():
celsius = float(temp_str[:-1])
fahrenheit = celsius_to_fahrenheit(celsius)
return f"{celsius} °C = {fahrenheit:.2f} °F"
elif 'f' in temp_str.lower():
fahrenheit = float(temp_str[:-1])
celsius = fahrenheit_to_celsius(fahrenheit)
return f"{fahrenheit} °F = {celsius:.2f} °C"
else:
return "入力が無効です。摂氏は '°C' を、華氏は '°F' を付けてください。"
# ユーザー入力例
user_input = input("温度を入力してください(例: 25°C, 77°F): ")
print(convert_temperature(user_input))
プログラムコードの説明
このコードでは、ユーザーが、摂氏'°C' を入力すると華氏'°F' が出力される。
反対に、ユーザーが華氏'°F' を入力すると、摂氏'°C' が出力される仕組みだ。
注意点:数字とCや、数字とFを入力しないと、エラーが出てしまう。
出力結果
温度を入力してください(例: 25°C, 77°F): 80F
80.0 °F = 26.67 °C
温度を入力してください(例: 25°C, 77°F): 90F
90.0 °F = 32.22 °C
温度を入力してください(例: 25°C, 77°F): 100F
100.0 °F = 37.78 °C
温度を入力してください(例: 25°C, 77°F): 30C
30.0 °C = 86.00 °F
温度を入力してください(例: 25°C, 77°F): 41.1C
41.1 °C = 105.98 °F
温度を入力してください(例: 25°C, 77°F): 38.5C
38.5 °C = 101.30 °F
このように、ちゃんとユーザが摂氏の気温を入力したら華氏の気温が出力され、
華氏の気温を入力したら摂氏が出力される。
皆さんも時間がある時に、このプログラムコードを参考にしながら、摂氏の気温を華氏に変換してみたり、華氏の気温を摂氏に変換してみてはいかがでしょうか。