栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python温度转换程序

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python温度转换程序

【问题描述】

        在温度刻画的不同体系中,摄氏度以1标准大气压下水的结冰点为0度,沸点为100度。华氏度以1标准大气压下水的结冰点为32度,沸点为212度。如何利用程序辅助进行摄氏度和华氏度之间的转换

【设计算法】

        根据华氏和摄氏温度定义,其单位刻度对应温度关系为(212-32)/(100-0)=1.8,转换公式如下:

【输入形式】

        输入温度加温度制式的代表字母

【输出形式】

        转换后的温度加温度制式的代表字母

【样例输入输出1】

What is the temperature?82F

The converted temperature is 28C

【样例输入输出2】

What is the temperature?28C

The converted temperature is 82F

【参考代码1】

TempStr = input("What is the temperature?")
if TempStr[-1] in ['F', 'f']:
    C = (eval(TempStr[0:-1]) - 32) / 1.8
    print("The converted temperature is {:d}C".format(int(C)))
elif TempStr[-1] in ['C', 'c']:
    F = 1.8 * eval(TempStr[0:-1]) + 32
    print("The converted temperature is {:d}F".format(int(F)))
else:
    print("Error")

【参考代码2】

input_str = input("What is the temperature?")
if input_str[-1] in ['C', 'c']:
    f = 1.8 * eval(input_str[0:-1]) + 32
    print("The converted temperature is %dF" % f)
elif input_str[-1] in ['F', 'f']:
    c = (eval(input_str[0:-1]) - 32) / 1.8
    print("The converted temperature is %dC" % c)
else:
    print("Input is wrong!")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/689055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号