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中的split()函数的使用方法
Apr 07 Python
基于python 爬虫爬到含空格的url的处理方法
May 11 Python
Python集中化管理平台Ansible介绍与YAML简介
Jun 12 Python
python使用minimax算法实现五子棋
Jul 29 Python
使用Python构造hive insert语句说明
Jun 06 Python
Python+PyQt5+MySQL实现天气管理系统
Jun 16 Python
keras使用Sequence类调用大规模数据集进行训练的实现
Jun 22 Python
python使用nibabel和sitk读取保存nii.gz文件实例
Jul 01 Python
Python 使用dict实现switch的操作
Apr 07 Python
python Polars库的使用简介
Apr 21 Python
Python标准库pathlib操作目录和文件
Nov 20 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
让codeigniter与swfupload整合的最佳解决方案
2014/06/12 PHP
destoon复制新模块的方法
2014/06/21 PHP
php把时间戳转换成多少时间之前函数的实例
2016/11/16 PHP
Javascript remove 自定义数组删除方法
2009/10/20 Javascript
jQuery 扩展对input的一些操作方法
2009/10/30 Javascript
从零开始学习jQuery (十) jQueryUI常用功能实战
2011/02/23 Javascript
JS实现点击下载的小例子
2013/07/10 Javascript
javascript拖拽上传类库DropzoneJS使用方法
2013/12/05 Javascript
浅析Node在构建超媒体API中的作用
2014/07/30 Javascript
基于jQuery实现的向下滑动二级菜单效果代码
2015/08/31 Javascript
jquery中object对象循环遍历的方法
2015/12/18 Javascript
JS中数组重排序方法
2016/11/11 Javascript
详解angularjs中如何实现控制器和指令之间交互
2017/05/31 Javascript
js实现导航跟随效果
2018/11/17 Javascript
vue项目中实现的微信分享功能示例
2019/01/21 Javascript
Element实现表格分页数据选择+全选所有完善批量操作
2019/06/07 Javascript
[01:16:12]完美世界DOTA2联赛PWL S2 FTD vs Inki 第一场 11.21
2020/11/23 DOTA
[35:55]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.11
2020/12/13 DOTA
python获取图片颜色信息的方法
2015/03/18 Python
python 将字符串完成特定的向右移动方法
2019/06/11 Python
详解Python3 pickle模块用法
2019/09/16 Python
python中对_init_的理解及实例解析
2019/10/11 Python
Python实现病毒仿真器的方法示例(附demo)
2020/02/19 Python
tensorflow实现从.ckpt文件中读取任意变量
2020/05/26 Python
pyqt5实现井字棋的示例代码
2020/12/07 Python
银行财务部实习生的自我鉴定
2013/11/27 职场文书
工作失职检讨书范文
2014/01/16 职场文书
会计与出纳自荐书范文
2014/03/16 职场文书
企业优秀员工事迹材料
2014/05/28 职场文书
2014年信贷员工作总结
2014/11/18 职场文书
先进教师个人主要事迹材料
2015/11/03 职场文书
外出培训学习心得体会
2016/01/18 职场文书
vite+vue3.0+ts+element-plus快速搭建项目的实现
2021/06/24 Vue.js
基于Python实现一个春节倒计时脚本
2022/01/22 Python
「我的青春恋爱物语果然有问题。-妄言录-」第20卷封面公开
2022/03/21 日漫
《勇者辞职不干了》ED主题曲无字幕动画MV公开
2022/04/13 日漫