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 IDLE加上自动补全和历史功能
Nov 30 Python
详解python3百度指数抓取实例
Dec 12 Python
解决python文件字符串转列表时遇到空行的问题
Jul 09 Python
python遍历文件夹下所有excel文件
Jan 03 Python
python中virtualenvwrapper安装与使用
May 20 Python
python实现维吉尼亚算法
Mar 20 Python
Python3多线程版TCP端口扫描器
Aug 31 Python
python实现用类读取文件数据并计算矩形面积
Jan 18 Python
tensorflow自定义激活函数实例
Feb 04 Python
python3 os进行嵌套操作的实例讲解
Nov 19 Python
Python读写锁实现实现代码解析
Nov 28 Python
python实现模拟器爬取抖音评论数据的示例代码
Jan 06 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
神族 Protoss 历史背景
2020/03/14 星际争霸
PHP保存带BOM文件的方法
2015/02/12 PHP
php动态读取数据清除最右边距的方法
2017/04/12 PHP
PHP设计模式之命令模式示例详解
2020/12/20 PHP
JavaScript 学习 - 提高篇
2007/02/02 Javascript
ExtJS Store的数据访问与更新问题
2010/04/28 Javascript
jQuery中even选择器的定义和用法
2014/12/23 Javascript
JS选项卡动态替换banner图片路径的方法
2015/05/11 Javascript
jQuery日历插件datepicker用法详解
2016/03/03 Javascript
JS使用eval()动态创建变量的方法
2016/06/03 Javascript
JS实现的简单轮播图运动效果示例
2016/12/22 Javascript
js原生Ajax的封装和原理详解
2017/03/11 Javascript
JavaScript深拷贝和浅拷贝概念与用法实例分析
2018/06/07 Javascript
JavaScript定时器常见用法实例分析
2019/11/15 Javascript
JS如何实现网站中PC端和手机端自动识别并跳转对应的代码
2020/01/08 Javascript
如何基于filter实现网站整体变灰功能
2020/04/17 Javascript
[42:27]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#2Fnatic VS OG第三局
2016/03/05 DOTA
[01:05:52]DOTA2-DPC中国联赛 正赛 Ehome vs Aster BO3 第一场 2月2日
2021/03/11 DOTA
Python实现的多项式拟合功能示例【基于matplotlib】
2018/05/15 Python
python如何创建TCP服务端和客户端
2018/08/26 Python
python虚拟环境迁移方法
2019/01/03 Python
浅谈Python中eval的强大与危害
2019/03/13 Python
python仿抖音表白神器
2019/04/08 Python
Python GUI编程学习笔记之tkinter界面布局显示详解
2020/03/30 Python
基于python SMTP实现自动发送邮件教程解析
2020/06/02 Python
matplotlib 范围选区(SpanSelector)的使用
2021/02/24 Python
STP协议的主要用途是什么?为什么要用STP
2012/12/20 面试题
主管会计岗位责任制
2014/02/10 职场文书
幼儿园元旦亲子活动方案
2014/02/17 职场文书
学员自我鉴定
2014/03/19 职场文书
公司副总经理任命书
2014/06/05 职场文书
教师求职信
2014/06/17 职场文书
《中国梦我的梦》小学生演讲稿
2014/08/20 职场文书
房屋鉴定委托书范本
2014/09/23 职场文书
现实表现材料范文
2014/12/23 职场文书
pytorch 中nn.Dropout的使用说明
2021/05/20 Python