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自动扫雷实现方法
Jul 25 Python
Python Unittest根据不同测试环境跳过用例的方法
Dec 16 Python
python 多个参数不为空校验方法
Feb 14 Python
在python里协程使用同步锁Lock的实例
Feb 19 Python
python判断链表是否有环的实例代码
Jan 31 Python
使用Python求解带约束的最优化问题详解
Feb 11 Python
Python图像处理库PIL中图像格式转换的实现
Feb 26 Python
Python利用matplotlib绘制折线图的新手教程
Nov 05 Python
python 基于opencv操作摄像头
Dec 24 Python
python绘制高斯曲线
Feb 19 Python
python scipy 稀疏矩阵的使用说明
May 26 Python
python实现双向链表原理
May 25 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下批量挂马和批量清马代码
2011/02/27 PHP
php 模拟 asp.net webFrom 按钮提交事件实例
2014/10/13 PHP
php实现常见图片格式的水印和缩略图制作(面向对象)
2016/06/15 PHP
PHP实现websocket通信的方法示例
2018/08/28 PHP
JS getMonth()日期函数的值域是0-11
2010/02/15 Javascript
jquery 插件学习(一)
2012/08/06 Javascript
jQuery获得IE版本不准确webbrowser的解决方法
2014/02/23 Javascript
js实现动画特效的文字链接鼠标悬停提示的方法
2015/03/02 Javascript
js获取及判断键盘按键的方法
2015/12/01 Javascript
jQuery增加、删除及修改select option的方法
2016/08/19 Javascript
JavaScript组成、引入、输出、运算符基础知识讲解
2016/12/08 Javascript
你有必要知道的10个JavaScript难点
2017/07/25 Javascript
Vue源码学习之初始化模块init.js解析
2017/11/02 Javascript
深入理解JavaScript和TypeScript中的class
2018/04/22 Javascript
JavaScript中var的重要性实例分析
2019/07/09 Javascript
简述ES6新增关键字let与var的区别
2019/08/23 Javascript
[01:00:54]TI4正赛第二日开场
2014/07/20 DOTA
Python实现类继承实例
2014/07/04 Python
python查询sqlite数据表的方法
2015/05/08 Python
解决python大批量读写.doc文件的问题
2018/05/08 Python
利用Python进行数据可视化常见的9种方法!超实用!
2018/07/11 Python
python分布式计算dispy的使用详解
2019/12/22 Python
Python Socketserver实现FTP文件上传下载代码实例
2020/03/27 Python
python 写一个水果忍者游戏
2021/01/13 Python
美国生日蛋糕店:Bake Me A Wish!
2017/02/08 全球购物
COACH德国官方网站:纽约现代奢侈品牌,1941年
2018/06/09 全球购物
GoPro摄像机美国官网:美国运动相机厂商
2018/07/03 全球购物
linux面试题参考答案(7)
2012/10/29 面试题
opencv实现图像几何变换
2021/03/24 Python
信用社实习人员自我鉴定
2013/09/20 职场文书
一帮一活动总结
2014/05/08 职场文书
单位单身证明样本
2014/10/11 职场文书
工作检讨书500字
2014/10/19 职场文书
流动人口婚育证明
2014/10/19 职场文书
六一活动主持词
2015/06/30 职场文书
2015年大学迎新晚会总结
2015/07/16 职场文书