Python 实例4 温度换算

该程序使用Python编写,提供了一个摄氏温度到华氏温度、开氏温度、列氏温度和兰金温度的转换功能。用户可以输入摄氏温度值和目标温度类型,程序会进行相应的计算并输出转换结果。如果输入非数字,程序会提示重新输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过输入摄氏温度及需要转换的类型,将摄氏温度转换为其他类型温度。

9ccd938a32936a883e036623e03abcdc.png

temp_type = ['摄氏温度', '华氏温度', '开氏温度', '列氏温度', '兰金温度']




def is_digit(temp):
    try:
        float(temp)
        return True
    except ValueError:
        pass


    try:
        import unicodedata
        unicodedata.numeric(temp)
        return True
    except (TypeError, ValueError):
        pass


    return False




def temperature_trans(temp, temp_type):
    if temp_type == '1':
        result = float(temp)
    elif temp_type == '2':
        result = float(temp) * 1.8 + 32
    elif temp_type == '3':
        result = float(temp) + 273.15
    elif temp_type == '4':
        result = float(temp) * 0.8
    elif temp_type == '5':
        result = (float(temp) + 237.15) * 1.8


    return result




if __name__ == '__main__':
    print('*' * 40)
    print(' ' * 12, "摄氏温度转换器", ' ' * 17)
    print('*' * 40)


    while True:
        temp = input("Input temperature('q' to quit):")
        if temp == 'q':
            break


        if not is_digit(temp):
            print("Please input the right centigrade!\n")
            continue


        temp_type_index = input("Input destination temperature type:\n"
                                "1 for '摄氏温度'\n 2 for '华氏温度'\n "
                                "3 for '开氏温度'\n 4 for '列氏温度'\n "
                                "5 for '兰金温度'\n Type=")


        result = temperature_trans(temp, temp_type_index)
        trans_result = temp_type[int(temp_type_index) - 1] + " {:.2f} ".format(result)
        print(trans_result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值