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中itertools模块用法详解
Sep 25 Python
wxPython定时器wx.Timer简单应用实例
Jun 03 Python
浅析python协程相关概念
Jan 20 Python
详解Django之admin组件的使用和源码剖析
May 04 Python
Pycharm设置界面全黑的方法
May 23 Python
python使用pymongo操作mongo的完整步骤
Apr 13 Python
在python中用url_for构造URL的方法
Jul 25 Python
Python中的 sort 和 sorted的用法与区别
Aug 10 Python
布隆过滤器的概述及Python实现方法
Dec 08 Python
简单了解Python读取大文件代码实例
Dec 18 Python
Django drf请求模块源码解析
Jun 08 Python
Python实现信息管理系统
Jun 05 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优于Node.js的五大理由分享
2012/09/15 PHP
php获取用户浏览器版本的方法
2015/01/03 PHP
Yii2第三方类库插件Imagine的安装和使用
2017/07/06 PHP
基于php流程控制语句和循环控制语句(讲解)
2017/10/23 PHP
javascript 读取图片文件的大小
2009/06/25 Javascript
js获取图片大小的函数代码
2011/09/20 Javascript
解决js正则匹配换行问题实现代码
2012/12/10 Javascript
JS取文本框中最小值的简单实例
2013/11/29 Javascript
js键盘上下左右键怎么触发function(实例讲解)
2013/12/14 Javascript
js网页实时倒计时精确到秒级
2014/02/10 Javascript
js防止DIV布局滚动时闪动的解决方法
2014/10/30 Javascript
JavaScript中数据类型转换总结
2016/12/25 Javascript
EasyUI折叠表格层次显示detailview详解及实例
2016/12/28 Javascript
Three.js基础部分学习
2017/01/08 Javascript
JavaScript设计模式之策略模式详解
2017/06/09 Javascript
ajax请求data遇到的问题分析
2018/01/18 Javascript
javascript与PHP动态往类中添加方法对比
2018/03/21 Javascript
Vue项目报错:Uncaught SyntaxError: Unexpected token
2018/11/10 Javascript
jQuery删除/清空指定元素的所有子节点实例代码
2019/07/04 jQuery
Node.js系列之发起get/post请求(2)
2019/08/30 Javascript
vue路由守卫,限制前端页面访问权限的例子
2019/11/11 Javascript
Python中表示字符串的三种方法
2017/09/06 Python
python opencv实现运动检测
2018/07/10 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
2020/10/30 Python
Beach Bunny Swimwear官网:设计师泳装和性感比基尼
2019/03/13 全球购物
世界上最大的铁人三项商店:Tri UK
2020/11/04 全球购物
口腔工艺技术专业毕业生自荐信
2013/09/27 职场文书
创业大赛策划书
2014/03/01 职场文书
贷款委托书范本
2014/04/08 职场文书
创意婚礼策划方案
2014/05/18 职场文书
承诺书范文
2014/06/03 职场文书
建筑工地宣传标语
2014/06/18 职场文书
民主生活会意见
2015/06/05 职场文书
2017元旦、春节期间廉洁自律承诺书
2016/03/25 职场文书
PostgreSQL13基于流复制搭建后备服务器的方法
2022/01/18 PostgreSQL
MySQL聚簇索引和非聚簇索引的区别详情
2022/06/14 MySQL