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学习笔记(二)基础语法
Jun 06 Python
详解python的几种标准输出重定向方式
Aug 15 Python
python实现读取并显示图片的两种方法
Jan 13 Python
python使用matplotlib库生成随机漫步图
Aug 27 Python
python3.4爬虫demo
Jan 22 Python
对python文件读写的缓冲行为详解
Feb 13 Python
python Django的web开发实例(入门)
Jul 31 Python
Python学习笔记之文件的读写操作实例分析
Aug 07 Python
利用 Flask 动态展示 Pyecharts 图表数据方法小结
Sep 04 Python
关于Python内存分配时的小秘密分享
Sep 05 Python
详解Python Opencv和PIL读取图像文件的差别
Dec 27 Python
pycharm设置当前工作目录的操作(working directory)
Feb 14 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
这部好评如潮的动漫 知名梗频出 但是画风劝退很多人
2020/03/08 日漫
叫你如何修改Nginx与PHP的文件上传大小限制
2014/09/10 PHP
PHP原生函数一定好吗?
2014/12/08 PHP
PHP实现的构造sql语句类实例
2016/02/03 PHP
php格式化时间戳
2016/12/17 PHP
JQuery this 和 $(this) 的区别
2009/08/23 Javascript
javascript 触发HTML元素绑定的函数
2010/09/11 Javascript
客户端限制只能上传jpg格式图片的js代码
2010/12/09 Javascript
jQuery构造函数init参数分析
2015/05/13 Javascript
jQuery实现页面顶部显示的进度条效果完整实例
2015/12/09 Javascript
bootstrap输入框组代码分享
2016/06/07 Javascript
AngularJS ng-bind 指令简单实现
2016/07/30 Javascript
Vue2.5 结合 Element UI 之 Table 和 Pagination 组件实现分页功能
2018/01/26 Javascript
JS实现获取毫秒值及转换成年月日时分秒的方法
2018/08/15 Javascript
基于AngularJS拖拽插件ngDraggable.js实现拖拽排序功能
2019/04/02 Javascript
vue实现动态给id赋值,点击事件获取当前点击的元素的id操作
2020/11/09 Javascript
用Python解析XML的几种常见方法的介绍
2015/04/09 Python
python实现SOM算法
2018/02/23 Python
python GUI实现小球满屏乱跑效果
2019/05/09 Python
详解django中Template语言
2020/02/22 Python
Python 使用Opencv实现目标检测与识别的示例代码
2020/09/08 Python
html5 分层屏幕适配的方法
2018/03/16 HTML / CSS
美国百年历史早餐食品供应商:Wolferman’s
2017/01/18 全球购物
Coach澳大利亚官方网站:美国著名时尚奢侈品牌
2017/05/24 全球购物
Otticanet澳大利亚:最顶尖的世界名牌眼镜, 能得到打折季的价格
2018/08/23 全球购物
华为python面试题
2016/05/03 面试题
公司大门门卫岗位职责
2014/06/11 职场文书
优秀团员事迹材料1000字
2014/08/20 职场文书
刑事辩护授权委托书
2014/09/13 职场文书
国庆节标语大全
2014/10/08 职场文书
2015年小学语文教学工作总结
2015/05/25 职场文书
2015年图书馆个人工作总结
2015/05/26 职场文书
培养联系人考察意见
2015/06/01 职场文书
2015年度女工工作总结
2015/10/22 职场文书
小学班主任研修日志
2015/11/13 职场文书
pytorch中[..., 0]的用法说明
2021/05/20 Python