Pythonで温度プログラムを書く
次のコードを使って、簡単な温度変換プログラムを書くことができます:
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
# 输入摄氏温度
celsius = float(input("请输入摄氏温度:"))
# 调用函数将摄氏温度转换为华氏温度
fahrenheit = celsius_to_fahrenheit(celsius)
# 输出转换结果
print("华氏温度为:", fahrenheit)
# 输入华氏温度
fahrenheit = float(input("请输入华氏温度:"))
# 调用函数将华氏温度转换为摄氏温度
celsius = fahrenheit_to_celsius(fahrenheit)
# 输出转换结果
print("摄氏温度为:", celsius)
摂氏温度を華氏温度に変換する関数と華氏温度を摂氏温度に変換する関数の2種類の関数を保持するプログラムで、メインプログラムで摂氏温度を入力し、摂氏温度から華氏温度に変換するcelsius_to_fahrenheit()関数を呼び出して結果を出力し、さらに華氏温度を入力し、華氏温度から摂氏温度に変換するfahrenheit_to_celsius()関数を呼び出して結果を出力する。