Python 温度转换:摄氏度和华氏度互转
华氏温度 ('F') 与摄氏温度 ('C') 的转换公式如下:
摄氏温度转换为华氏温度:F = C * 9/5 + 32
华氏温度转换为摄氏温度:C = (F - 32) * 5/9
以下是 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 = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print(f'{celsius}摄氏度 = {fahrenheit}华氏度')
fahrenheit = 77
celsius = fahrenheit_to_celsius(fahrenheit)
print(f'{fahrenheit}华氏度 = {celsius}摄氏度')
原文地址: https://www.cveoy.top/t/topic/ck8K 著作权归作者所有。请勿转载和采集!