python用函数实现温度转换。实现华氏度、摄氏度之间的相互转换。其中,摄氏度 = (华氏度 - 32) / 1.8;华氏度 = 摄氏度*1.8 + 32。

def celsius_to_fahrenheit(celsius):
    """
    将摄氏度转换为华氏度
    """
    fahrenheit = celsius * 1.8 + 32
    return fahrenheit

def fahrenheit_to_celsius(fahrenheit):
    """
    将华氏度转换为摄氏度
    """
    celsius = (fahrenheit - 32) / 1.8
    return celsius

# 输入温度和温度单位
temperature = float(input('请输入温度:'))
unit = input('请输入温度单位(C表示摄氏度,F表示华氏度):')

# 根据温度单位调用不同的函数进行转换
if unit == 'C':
    fahrenheit = celsius_to_fahrenheit(temperature)
    print(f'{temperature:.2f}摄氏度 = {fahrenheit:.2f}华氏度')
elif unit == 'F':
    celsius = fahrenheit_to_celsius(temperature)
    print(f'{temperature:.2f}华氏度 = {celsius:.2f}摄氏度')
else:
    print('无法识别温度单位')

这个程序使用了两个函数 celsius_to_fahrenheit 和 fahrenheit_to_celsius,用来进行摄氏度和华氏度之间的转换。在主程序中,我们首先输入一个温度和温度单位。然后根据温度单位调用不同的函数进行转换,并输出转换后的结果。注意,我们使用了格式化字符串来控制输出的小数位数。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值