欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)簡單溫度轉(zhuǎn)換的方法

 更新時間:2015年03月13日 11:57:16   作者:niuniu  
這篇文章主要介紹了python實現(xiàn)簡單溫度轉(zhuǎn)換的方法,涉及Python操作字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了python實現(xiàn)簡單溫度轉(zhuǎn)換的方法。分享給大家供大家參考。具體分析如下:

這是一段簡單的python代碼,用戶轉(zhuǎn)換不同單位的溫度,適合初學(xué)者參考

復(fù)制代碼 代碼如下:
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程序設(shè)計有所幫助。

您可能感興趣的文章:

相關(guān)文章

  • Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換

    Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換

    這篇文章主要介紹了Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換,JSON的全稱是 JavaScript Object Notation,是一種輕量級的數(shù)據(jù)交換格式,關(guān)于JSON的更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下
    2022-06-06
  • Python SMTP發(fā)送電子郵件的示例

    Python SMTP發(fā)送電子郵件的示例

    這篇文章主要介紹了Python SMTP發(fā)送電子郵件的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • Python中?*?號的用法總結(jié)

    Python中?*?號的用法總結(jié)

    Python中的?*號是一個特殊的符號,在其他編程語言中,它最廣為人知的用途就是作為乘法運算的符號,本文總結(jié)了Python中*號的所有用途,希望對大家有所幫助
    2023-11-11
  • Django實現(xiàn)發(fā)送郵件功能

    Django實現(xiàn)發(fā)送郵件功能

    這篇文章主要介紹了Django實現(xiàn)發(fā)送郵件功能,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • 如何提高python 中for循環(huán)的效率

    如何提高python 中for循環(huán)的效率

    這篇文章主要介紹了如何提高python 中for循環(huán)的效率,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Pycharm設(shè)置utf-8自動顯示方法

    Pycharm設(shè)置utf-8自動顯示方法

    今天小編就為大家分享一篇Pycharm設(shè)置utf-8自動顯示方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • pytest自動化測試中的fixture的聲明和調(diào)用

    pytest自動化測試中的fixture的聲明和調(diào)用

    這篇文章主要為大家介紹了pytest自動化測試中的fixture的聲明和調(diào)用,文中含有詳細示例操作有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Python中常見的加密解密算法總結(jié)

    Python中常見的加密解密算法總結(jié)

    這篇文章主要為大家詳細介紹了Python中常見的一些加密解密算法的實現(xiàn),文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2023-03-03
  • 詳解anaconda離線安裝pytorchGPU版

    詳解anaconda離線安裝pytorchGPU版

    這篇文章主要介紹了詳解anaconda離線安裝pytorchGPU版,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python命令行參數(shù)argparse模塊基本用法詳解

    python命令行參數(shù)argparse模塊基本用法詳解

    argparse?是python自帶的命令行參數(shù)解析包,可以用來方便地讀取命令行參數(shù),這篇文章主要介紹了python命令行參數(shù)-argparse模塊基本用法,需要的朋友可以參考下
    2023-01-01

最新評論