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中的wxPython实现最基本的浏览器功能
Apr 14 Python
使用Python构建Hopfield网络的教程
Apr 14 Python
一百多行python代码实现抢票助手
Sep 25 Python
Python中staticmethod和classmethod的作用与区别
Oct 11 Python
python-pyinstaller、打包后获取路径的实例
Jun 10 Python
对pyqt5多线程正确的开启姿势详解
Jun 14 Python
使用celery和Django处理异步任务的流程分析
Feb 19 Python
MAC平台基于Python Appium环境搭建过程图解
Aug 13 Python
python 如何使用find和find_all爬虫、找文本的实现
Oct 16 Python
利用python为PostgreSQL的表自动添加分区
Jan 18 Python
python opencv人脸识别考勤系统的完整源码
Apr 26 Python
分享提高 Python 代码的可读性的技巧
Mar 03 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 foreach、while性能比较
2009/10/15 PHP
php 魔术方法详解
2014/11/11 PHP
php实现的中文分词类完整实例
2017/02/06 PHP
php 中self,this的区别和操作方法实例分析
2019/11/04 PHP
农历与西历对照
2006/09/06 Javascript
JavaScript日历实现代码
2010/09/12 Javascript
jquery索引在使用中的一些困惑
2013/10/24 Javascript
js生成动态表格并为每个单元格添加单击事件的方法
2014/04/14 Javascript
基于javascript制作经典传统的拼图游戏
2016/03/22 Javascript
JavaScript必知必会(三) String .的方法来自何方
2016/06/08 Javascript
Nodejs 发送Post请求功能(发短信验证码例子)
2017/02/09 NodeJs
从零学习node.js之详解异步控制工具async(八)
2017/02/27 Javascript
实例解析ES6 Proxy使用场景介绍
2018/01/08 Javascript
使用vue-cli(vue脚手架)快速搭建项目的方法
2018/05/21 Javascript
利用vue-i18n实现多语言切换效果的方法
2019/06/19 Javascript
解决vue打包后vendor.js文件过大问题
2019/07/03 Javascript
js实现漂亮的星空背景
2019/11/01 Javascript
小程序富文本提取图片可放大缩小
2020/05/26 Javascript
Python psutil模块简单使用实例
2015/04/28 Python
修复CentOS7升级Python到3.6版本后yum不能正确使用的解决方法
2018/01/26 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
2018/12/12 Python
Python命名空间的本质和加载顺序
2018/12/17 Python
在Python中调用Ping命令,批量IP的方法
2019/01/26 Python
Python安装selenium包详细过程
2019/07/23 Python
python 中值滤波,椒盐去噪,图片增强实例
2019/12/18 Python
在python3中实现查找数组中最接近与某值的元素操作
2020/02/29 Python
Python 统计位数为偶数的数字代码详解
2020/03/15 Python
完美解决IE8下不兼容rgba()的问题
2017/03/31 HTML / CSS
详解Canvas 跨域脱坑实践
2018/11/07 HTML / CSS
无偿献血倡议书
2014/04/14 职场文书
《永远的白衣战士》教学反思
2014/04/25 职场文书
小学生我的梦想演讲稿
2014/08/21 职场文书
严以修身专题学习研讨会发言材料
2015/11/09 职场文书
初中英语教学反思范文
2016/02/15 职场文书
会计专业2019暑假实习报告
2019/06/21 职场文书
python超详细实现完整学生成绩管理系统
2022/03/17 Python