国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 17:57:09
字體:
供稿:網(wǎng)友

本文實例講述了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è)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 舟山市| 澄城县| 盐亭县| 焦作市| 双牌县| 林芝县| 正安县| 稷山县| 阳东县| 博客| 宜宾县| 大姚县| 保靖县| 克拉玛依市| 上蔡县| 昆山市| 蚌埠市| 寿宁县| 石狮市| 大石桥市| 海盐县| 北安市| 丹寨县| 方正县| 和静县| 郴州市| 龙里县| 轮台县| 紫阳县| 玛纳斯县| 正镶白旗| 华坪县| 政和县| 漾濞| 四会市| 长乐市| 普安县| 大英县| 洪雅县| 遂溪县| 墨竹工卡县|