python实现简单温度转换的方法

作者:niuniu 时间:2021-04-12 10:14:51 

本文实例讲述了python实现简单温度转换的方法。分享给大家供大家参考。具体分析如下:

这是一段简单的python代码,用户转换不同单位的温度,适合初学者参考

def c2f(t):
    return (t*9/5.0)+32
def c2k(t):
    return t+273.15
def f2c(t):
    return (t-32)*5.0/9
def f2k(t):
    return (t+459.67)*5.0/9
def k2c(t):
    return t-273.15
def k2f(t):
    return (t*9/5.0)-459.67
def get_user_input():
    user_input = 0
    while type(user_input) != type(1.0):
        user_input = raw_input("Enter degrees to convert: ")
        try:
            user_input = float(user_input)
        except:
            print user_input + " is not a valid entry"
    return user_input
def main():
    menu = "\nTemperature Convertor\n\n"+\
        "1. Celsius to Fahrenheit\n"+\
        "2. Celsius to Kelvin\n"+\
        "3. Fahrenheit to Celsius\n"+\
        "4. Fahrenheit to Kelvin\n"+\
        "5. Kelvin to Celsius\n"+\
            "6. Kelvin to Fahrenheit\n"+\
        "7. Quit"
    user_input = 0
    while user_input != 7:
        print menu
        user_input = raw_input("Please enter a valid selection: ")
        try:
            user_input = int(user_input)
        except:
            print user_input + " is not a valid selction, please try again\n"
        if user_input == 1:
            t = get_user_input()
            print str(t) + " degree Celsius is " + str((c2f(t))) + " degree Fahrenheit"
        elif user_input == 2:
            t = get_user_input()
            print str(t) + " degree Celsius is " + str((c2k(t))) + " degree Kelvin"
        elif user_input == 3:
            t = get_user_input()
            print str(t) + " degree Fahrenheit is " + str((f2c(t))) + " degree Celsius"
        elif user_input == 4:
            t = get_user_input()
            print str(t) + " degree Fahrenheit is " + str((f2K(t))) + " degree Kelvin"
        elif user_input == 5:
            t = get_user_input()
            print str(t) + " degree Kelvin is " + str((k2c(t))) + " degree Celsius"
        elif user_input == 6:
            t = get_user_input()
            print str(t) + " degree Kelvin is " + str((k2f(t))) + " degree Fahrenheit"
        elif user_input == 7:
            quit()
        else:
            print str(user_input) + " is not a valid selection, please try again\n"
if __name__ == "__main__":
    main()

希望本文所述对大家的Python程序设计有所帮助。

标签:python,温度,转换,方法
0
投稿

猜你喜欢

  • python 将字符串转换成字典dict

    2023-11-27 17:08:33
  • Python教程之成员和身份运算符的用法详解

    2021-04-19 11:36:56
  • ThinkPHP3.1.2 使用cli命令行模式运行的方法

    2023-11-14 12:56:27
  • Python asyncio异步编程常见问题小结

    2023-10-04 23:13:56
  • 使用setup.py安装python包和卸载python包的方法

    2023-01-31 02:20:47
  • bootstrap自定义样式之bootstrap实现侧边导航栏功能

    2023-09-06 20:41:36
  • Python pandas.DataFrame调整列顺序及修改index名的方法

    2023-11-22 20:53:59
  • numpy.random.shuffle打乱顺序函数的实现

    2021-02-04 07:19:34
  • 解决pytorch 保存模型遇到的问题

    2021-12-13 08:26:37
  • Python处理命令行参数模块optpars用法实例分析

    2021-07-20 05:27:36
  • 详解opencv去除背景算法的方法比较

    2022-10-08 08:48:08
  • python实现可视化动态CPU性能监控

    2023-08-08 10:23:52
  • 利用Python实现自动化监控文件夹完成服务部署

    2023-03-15 00:02:04
  • Python Socket编程入门教程

    2022-03-08 01:08:49
  • Python 读写 Matlab Mat 格式数据的操作

    2023-08-23 01:21:12
  • 使用SqlBulkCopy时应注意Sqlserver表中使用缺省值的列

    2012-07-11 15:34:35
  • python中使用smtplib和email模块发送邮件实例

    2022-09-16 05:37:23
  • python实现12306抢票及自动邮件发送提醒付款功能

    2021-03-12 19:01:46
  • Python实现图片滑动式验证识别方法

    2023-11-05 22:14:52
  • python基于pygame实现飞机大作战小游戏

    2021-04-27 07:59:17
  • asp之家 网络编程 m.aspxhome.com