python實(shí)現(xiàn)簡單溫度轉(zhuǎn)換的方法
本文實(shí)例講述了python實(shí)現(xiàn)簡單溫度轉(zhuǎn)換的方法。分享給大家供大家參考。具體分析如下:
這是一段簡單的python代碼,用戶轉(zhuǎn)換不同單位的溫度,適合初學(xué)者參考
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),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02python實(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-08Python實(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的解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Python通過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-03Python的Socket編程過程中實(shí)現(xiàn)UDP端口復(fù)用的實(shí)例分享
這篇文章主要介紹了Python的Socket編程過程中實(shí)現(xiàn)UDP端口復(fù)用的實(shí)例分享,文中作者用到了Python的twisted異步框架,需要的朋友可以參考下2016-03-03