python实现简单温度转换的方法

yipeiwu_com5年前Python基础

本文实例讲述了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在groupby分组后提取指定位置记录方法

Python在groupby分组后提取指定位置记录方法

在进行数据分析、数据建模时,我们首先要做的就是对数据进行处理,提取我们需要的信息。下面为大家介绍一些groupby的用法,以便能够更加方便地进行数据处理。 我们往往在使用groupby进...

pygame实现打字游戏

pygame实现打字游戏

本文实例为大家分享了pygame实现打字游戏的具体代码,供大家参考,具体内容如下 1.基本代码 下面的代码完成了每一秒在界面的顶部随机生成一个新的字母 # -*- coding=ut...

人工智能最火编程语言 Python大战Java!

人工智能最火编程语言 Python大战Java!

开发者到底应该学习哪种编程语言才能获得机器学习或数据科学这类工作呢?这是一个非常重要的问题。我们在许多论坛上都有讨论过。现在,我可以提供我自己的答案并解释原因,但我们先看一些数据。毕竟,...

django 实现编写控制登录和访问权限控制的中间件方法

django中,很多时候我们都需要有一个地方来进行更加详细的权限控制,例如说哪些用户可以访问哪些页面,检查登录状态等,这里的话就涉及到了中间件的编写了。 在django项目下的setti...

Python实现进程同步和通信的方法

Python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessi...