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

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

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

本文實(shí)例講述了python實(shí)現(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è)計(jì)有所幫助。

相關(guān)文章

  • python如何實(shí)現(xiàn)單鏈表的反轉(zhuǎn)

    python如何實(shí)現(xiàn)單鏈表的反轉(zhuǎn)

    這篇文章主要介紹了python如何實(shí)現(xiàn)單鏈表的反轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python語法教程之def()函數(shù)定義及用法

    python語法教程之def()函數(shù)定義及用法

    函數(shù)是組織好的,可重復(fù)使用的,用來實(shí)現(xiàn)單一,或相關(guān)聯(lián)功能的代碼段,下面這篇文章主要給大家介紹了關(guān)于python語法教程之def()函數(shù)定義及用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼

    python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python實(shí)戰(zhàn)小游戲飛機(jī)大戰(zhàn)詳解

    Python實(shí)戰(zhàn)小游戲飛機(jī)大戰(zhàn)詳解

    飛機(jī)大戰(zhàn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂的場景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個(gè)簡單的飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 解決python和pycharm安裝gmpy2 出現(xiàn)ERROR的問題

    解決python和pycharm安裝gmpy2 出現(xiàn)ERROR的問題

    這篇文章主要介紹了python和pycharm安裝gmpy2 出現(xiàn)ERROR的解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python和pyqt-tools安裝位置圖文詳解

    python和pyqt-tools安裝位置圖文詳解

    查詢Python安裝位置可通過命令行輸入import sys和sys.path實(shí)現(xiàn),而Python-tools的安裝位置則可在Python文件夾內(nèi)找到,本文介紹了如何查詢Python及其工具包PyQt-tools的安裝位置,便于用戶進(jìn)行相關(guān)設(shè)置或調(diào)試,更多關(guān)于Python安裝和配置的內(nèi)容,可查閱腳本之家的相關(guān)文章
    2024-09-09
  • Python通過TensorFlow卷積神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)貓狗識別

    Python通過TensorFlow卷積神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)貓狗識別

    今天小編就為大家分享一篇關(guān)于Python通過TensorFlow卷積神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)貓狗識別,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • python?類型轉(zhuǎn)換函數(shù)示例詳解

    python?類型轉(zhuǎn)換函數(shù)示例詳解

    這篇文章主要介紹了python?類型轉(zhuǎn)換函數(shù)示例詳解,通過float()將一個(gè)字符串或數(shù)字轉(zhuǎn)換為浮點(diǎn)數(shù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • python 平衡二叉樹實(shí)現(xiàn)代碼示例

    python 平衡二叉樹實(shí)現(xiàn)代碼示例

    這篇文章主要介紹了python 平衡二叉樹實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Python的Socket編程過程中實(shí)現(xiàn)UDP端口復(fù)用的實(shí)例分享

    Python的Socket編程過程中實(shí)現(xiàn)UDP端口復(fù)用的實(shí)例分享

    這篇文章主要介紹了Python的Socket編程過程中實(shí)現(xiàn)UDP端口復(fù)用的實(shí)例分享,文中作者用到了Python的twisted異步框架,需要的朋友可以參考下
    2016-03-03

最新評論