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网络爬虫功能的基本写法
Jan 28 Python
Python的Django中将文件上传至七牛云存储的代码分享
Jun 03 Python
Python利用BeautifulSoup解析Html的方法示例
Jul 30 Python
Tensorflow 自带可视化Tensorboard使用方法(附项目代码)
Feb 10 Python
python 绘制拟合曲线并加指定点标识的实现
Jul 10 Python
Django基础知识 URL路由系统详解
Jul 18 Python
python实现while循环打印星星的四种形状
Nov 23 Python
python飞机大战pygame碰撞检测实现方法分析
Dec 17 Python
使用Keras建立模型并训练等一系列操作方式
Jul 02 Python
详解torch.Tensor的4种乘法
Sep 03 Python
完美解决torch.cuda.is_available()一直返回False的玄学方法
Feb 06 Python
python 对xml解析的示例
Feb 27 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
DC漫画《蝙蝠侠和猫女》图透 猫女怀孕老爷当爹
2020/04/09 欧美动漫
编写漂亮的代码 - 将后台程序与前端程序分开
2008/04/23 PHP
浅析php变量作用域的一些问题
2013/08/08 PHP
php获取服务器端mac和客户端mac的地址支持WIN/LINUX
2014/05/15 PHP
PHP代码实现爬虫记录――超管用
2015/07/31 PHP
Symfony2安装的方法(2种方法)
2016/02/04 PHP
PHP读取zip文件的方法示例
2016/11/17 PHP
laravel5.5添加echarts实现画图功能的方法
2019/10/09 PHP
JavaScript事件委托的技术原理探讨示例
2014/04/17 Javascript
javascript操作符"!~"详解
2015/02/10 Javascript
Javascript数组Array方法解读
2016/03/13 Javascript
基于jquery实现ajax无刷新评论
2020/08/19 Javascript
WEB开发之注册页面验证码倒计时代码的实现
2016/12/15 Javascript
基于pako.js实现gzip的压缩和解压功能示例
2017/06/13 Javascript
微信小程序的日期选择器的实例详解
2017/09/29 Javascript
详解如何实现一个简单的Node.js脚手架
2017/12/04 Javascript
python抓取京东商城手机列表url实例代码
2013/12/18 Python
Python基于pygame实现的弹力球效果(附源码)
2015/11/11 Python
Python简单检测文本类型的2种方法【基于文件头及cchardet库】
2016/09/18 Python
用生成器来改写直接返回列表的函数方法
2017/05/25 Python
Python爬虫常用库的安装及其环境配置
2018/09/19 Python
python判断完全平方数的方法
2018/11/13 Python
对PyQt5中树结构的实现方法详解
2019/06/17 Python
django组合搜索实现过程详解(附代码)
2019/08/06 Python
Python 使用type来定义类的实现
2019/11/19 Python
快速实现一个简单的canvas迷宫游戏的示例
2018/07/04 HTML / CSS
Html5自定义字体解决方法
2019/10/09 HTML / CSS
英国男士时尚购物网站:Stuarts London
2017/10/22 全球购物
孕妇内衣和胸罩:Cake Maternity
2018/07/16 全球购物
LUISAVIAROMA德国官网:时尚奢侈品牌购物网站
2020/11/12 全球购物
生物化学研究助理员求职信
2013/10/09 职场文书
机械绘图员岗位职责
2013/11/19 职场文书
抗洪救灾先进集体事迹材料
2014/05/26 职场文书
工程部部长岗位职责
2015/02/12 职场文书
Django实现聊天机器人
2021/05/31 Python
Nginx四层负载均衡的配置指南
2021/06/11 Servers