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

 更新时间:2015年03月13日 11:57:16   作者:niuniu  
这篇文章主要介绍了python实现简单温度转换的方法,涉及Python操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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如何实现单链表的反转

    python如何实现单链表的反转

    这篇文章主要介绍了python如何实现单链表的反转,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • python语法教程之def()函数定义及用法

    python语法教程之def()函数定义及用法

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段,下面这篇文章主要给大家介绍了关于python语法教程之def()函数定义及用法的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • python实现每天自动签到领积分的示例代码

    python实现每天自动签到领积分的示例代码

    这篇文章主要介绍了python实现每天自动签到领积分的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Python实战小游戏飞机大战详解

    Python实战小游戏飞机大战详解

    飞机大战想必是很多人童年时期的经典游戏,我们依旧能记得抱个老人机娱乐的场景,下面这篇文章主要给大家介绍了关于如何利用python写一个简单的飞机大战小游戏的相关资料,需要的朋友可以参考下
    2021-11-11
  • 解决python和pycharm安装gmpy2 出现ERROR的问题

    解决python和pycharm安装gmpy2 出现ERROR的问题

    这篇文章主要介绍了python和pycharm安装gmpy2 出现ERROR的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • python和pyqt-tools安装位置图文详解

    python和pyqt-tools安装位置图文详解

    查询Python安装位置可通过命令行输入import sys和sys.path实现,而Python-tools的安装位置则可在Python文件夹内找到,本文介绍了如何查询Python及其工具包PyQt-tools的安装位置,便于用户进行相关设置或调试,更多关于Python安装和配置的内容,可查阅脚本之家的相关文章
    2024-09-09
  • Python通过TensorFlow卷积神经网络实现猫狗识别

    Python通过TensorFlow卷积神经网络实现猫狗识别

    今天小编就为大家分享一篇关于Python通过TensorFlow卷积神经网络实现猫狗识别,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • python 类型转换函数示例详解

    python 类型转换函数示例详解

    这篇文章主要介绍了python 类型转换函数示例详解,通过float()将一个字符串或数字转换为浮点数,本文结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • python 平衡二叉树实现代码示例

    python 平衡二叉树实现代码示例

    这篇文章主要介绍了python 平衡二叉树实现代码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Python的Socket编程过程中实现UDP端口复用的实例分享

    Python的Socket编程过程中实现UDP端口复用的实例分享

    这篇文章主要介绍了Python的Socket编程过程中实现UDP端口复用的实例分享,文中作者用到了Python的twisted异步框架,需要的朋友可以参考下
    2016-03-03

最新评论