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


Posted in Python onMarch 13, 2015

本文实例讲述了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入门篇之字符串
Oct 17 Python
Python读写ini文件的方法
May 28 Python
Python程序中设置HTTP代理
Nov 06 Python
python解析基于xml格式的日志文件
Feb 25 Python
python九九乘法表的实例
Sep 26 Python
python3设计模式之简单工厂模式
Oct 17 Python
python-itchat 统计微信群、好友数量,及原始消息数据的实例
Feb 21 Python
Python代码使用 Pyftpdlib实现FTP服务器功能
Jul 22 Python
让你的Python代码实现类型提示功能
Nov 19 Python
Python 代码调试技巧示例代码
Aug 11 Python
用基于python的appium爬取b站直播消费记录
Apr 17 Python
如何用 Python 子进程关闭 Excel 自动化中的弹窗
May 07 Python
python实现简单socket程序在两台电脑之间传输消息的方法
Mar 13 #Python
Python比较两个图片相似度的方法
Mar 13 #Python
python通过urllib2获取带有中文参数url内容的方法
Mar 13 #Python
python将MongoDB里的ObjectId转换为时间戳的方法
Mar 13 #Python
python通过正则查找微博@(at)用户的方法
Mar 13 #Python
python使用chardet判断字符串编码的方法
Mar 13 #Python
python根据时间生成mongodb的ObjectId的方法
Mar 13 #Python
You might like
PHP.ini中配置屏蔽错误信息显示和保存错误日志的例子
2014/05/12 PHP
Yii2创建控制器(createController)方法详解
2016/07/23 PHP
xml文档转换工具,附图表例子(hta)
2010/11/17 Javascript
返回页面顶部top按钮通过锚点实现(自写)
2013/08/30 Javascript
JavaScript 获取任一float型小数点后两位的小数
2014/06/30 Javascript
JavaScript数据结构和算法之图和图算法
2015/02/11 Javascript
简介AngularJS的HTML DOM支持情况
2015/06/17 Javascript
一张Web前端的思维导图分享
2015/07/03 Javascript
JS面向对象编程详解
2016/03/06 Javascript
canvas实现手机端用来上传用户头像的代码
2016/10/20 Javascript
vue 和vue-touch 实现移动端左右导航效果(仿京东移动站导航)
2017/04/22 Javascript
解决BootStrap Fileinput手机图片上传显示旋转问题
2017/06/01 Javascript
详解angularJs中关于ng-class的三种使用方式说明
2017/06/02 Javascript
js判断输入框不能为空格或null值的实现方法
2018/03/02 Javascript
video.js 实现视频只能后退不能快进的思路详解
2018/08/09 Javascript
3分钟了解vue数据劫持的原理实现
2019/05/01 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
js实现聊天对话框
2020/02/08 Javascript
[01:09]DOTAPLUS——DOTA2的新时代
2018/04/04 DOTA
[01:20:37]FNATIC vs NIP 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
Python编程之string相关操作实例详解
2017/07/22 Python
python中栈的原理及实现方法示例
2019/11/27 Python
提升python处理速度原理及方法实例
2019/12/25 Python
关于Python turtle库使用时坐标的确定方法
2020/03/19 Python
Python如何进行时间处理
2020/08/06 Python
用python读取xlsx文件
2020/12/17 Python
SpringBoot首页设置解析(推荐)
2021/02/11 Python
HTML5中的postMessage API基本使用教程
2016/05/20 HTML / CSS
房地产管理毕业生自荐信
2013/11/04 职场文书
计算机相关的自我评价
2014/01/15 职场文书
基层干部十八大感言
2014/01/19 职场文书
秸秆管理实施方案
2014/03/15 职场文书
活动总结报告怎么写
2014/07/03 职场文书
2015年清明节活动总结
2015/02/09 职场文书
小学生交通安全寄语
2015/02/27 职场文书
庭外和解协议书
2016/03/23 职场文书