基于Python實(shí)現(xiàn)溫度轉(zhuǎn)換程序
1.使用pycharm運(yùn)行溫度轉(zhuǎn)換程序,嘗試將溫度單位設(shè)在前面
2.參照溫度轉(zhuǎn)換程序,自己寫一個(gè)關(guān)于貨幣轉(zhuǎn)換、長(zhǎng)度轉(zhuǎn)換、重量轉(zhuǎn)換或者面積轉(zhuǎn)換的程序
循環(huán)+函數(shù)
def convertemperature():
temperature = ""
while (temperature != "q"):
temperature = input("請(qǐng)輸入帶有符號(hào)的溫度:")
if (temperature[-1] in ['f', 'F']):
C = (eval(temperature[0:-1]) - 32) / 1.8
print("轉(zhuǎn)換后的溫度是{:.2f}C".format(C))
print("退出請(qǐng)輸入q")
elif (temperature[-1] in ['c', 'C']):
C = (1.8 * eval(temperature[0:-1]) + 32)
print("轉(zhuǎn)換后的溫度是{:.2f}C".format(C))
else:
print("輸入格式錯(cuò)誤")
print("退出請(qǐng)輸入q")
convertemperature()循環(huán) +異常處理 溫度單位設(shè)在前面 f代表華氏度 c代表攝氏度---函數(shù)里面循環(huán)
def convertemperature():
temperature = ""
while (temperature != "exit"):
temperature = input("請(qǐng)輸入帶有符號(hào)的溫度:")
if (temperature[0] in ['f', 'F']):
try:
C = (eval(temperature[1:]) - 32) / 1.8
print("轉(zhuǎn)換后的溫度是{:.2f}C".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入exit")
elif (temperature[0] in ['c', 'C']):
try:
C = (1.8 * eval(temperature[1:]) + 32)
print("轉(zhuǎn)換后的溫度是{:.2f}C".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入exit")
else:
print("輸入格式錯(cuò)誤")
print("退出請(qǐng)輸入exit")
# 循環(huán) +異常處理 溫度單位設(shè)在前面 f代表華氏度 c代表攝氏度
convertemperature()循環(huán)里面使用函數(shù)
temperature=""
while (temperature != "q"):
temperature = input("請(qǐng)輸入帶有符號(hào)的溫度:")
convertemperature2(temperature)
def convertemperature2(aa):
temperature = aa
if (temperature[0] in ['f', 'F']):
try:
C = (eval(temperature[1:]) - 32) / 1.8
print("轉(zhuǎn)換后的溫度是{:.2f}C".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入q")
elif (temperature[0] in ['c', 'C']):
try:
C = (1.8 * eval(temperature[1:]) + 32)
print("轉(zhuǎn)換后的溫度是{:.2f}C".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入q")
else:
print("輸入格式錯(cuò)誤")
print("退出請(qǐng)輸入q")重量轉(zhuǎn)換
# 貨幣轉(zhuǎn)換、長(zhǎng)度轉(zhuǎn)換 、重量轉(zhuǎn)換或者面積轉(zhuǎn)換
temperature = ""
while temperature != "q":
temperature = input("請(qǐng)輸入帶有符號(hào)的重量:")
print(temperature[-2:])
if temperature[-2:] in ['kg', "Kg", "KG", "kG"]:
try:
C = (eval(temperature[0:-2]) * 1000)
print("轉(zhuǎn)換后的重量是{:.2f}克".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入q")
elif temperature[-1] in ['g', 'G']:
try:
C = (eval(temperature[0:-1]) / 1000)
print("轉(zhuǎn)換后的重量是{:.2f}千克".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入q")
else:
print("輸入格式錯(cuò)誤")
print("退出請(qǐng)輸入q")
# 循環(huán) +異常處理 重量轉(zhuǎn)換模仿例子程序,編寫英寸和厘米的轉(zhuǎn)換程序,1inch=2.54cm。比如,輸入3.2i則輸出8.13 c,輸入4.5c則輸出1.77i,輸入4.5k輸出“輸入格式錯(cuò)誤”。
temperature = ""
while temperature != "q":
temperature = input("請(qǐng)輸入帶有符號(hào)的尺寸:eg:3.2i \n")
if temperature[-1:] in ['i', 'I']:
try:
C = (eval(temperature[0:-1]) * 2.54)
print("轉(zhuǎn)換后是{:.2f}c".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入q")
elif temperature[-1] in ['c', 'C']:
try:
C = (eval(temperature[0:-1]) / 2.54)
print("轉(zhuǎn)換后是{:.2f}i".format(C))
except:
print("轉(zhuǎn)換失敗,請(qǐng)重新輸入")
print("退出請(qǐng)輸入q")
else:
print("輸入格式錯(cuò)誤")
print("退出請(qǐng)輸入q")到此這篇關(guān)于基于Python實(shí)現(xiàn)溫度轉(zhuǎn)換程序的文章就介紹到這了,更多相關(guān)Python溫度轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python django 實(shí)現(xiàn)驗(yàn)證碼的功能實(shí)例代碼
本篇文章主要介紹了python django 實(shí)現(xiàn)驗(yàn)證碼的功能實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
python可視化 matplotlib畫圖使用colorbar工具自定義顏色
這篇文章主要介紹了python可視化 matplotlib畫圖使用colorbar工具自定義顏色,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python常用模塊sys,os,time,random功能與用法實(shí)例分析
這篇文章主要介紹了Python常用模塊sys,os,time,random功能與用法,結(jié)合實(shí)例形式分析了Python模塊sys,os,time,random功能、原理、相關(guān)模塊函數(shù)、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
Python報(bào)錯(cuò)TypeError: unhashable type: ‘numpy.nd
在Python編程中,尤其是在處理數(shù)據(jù)時(shí),我們經(jīng)常使用numpy數(shù)組,然而,當(dāng)我們嘗試將numpy數(shù)組用作字典的鍵或集合的元素時(shí),就會(huì)遇到TypeError: unhashable type: 'numpy.ndarray',本文將探討這個(gè)錯(cuò)誤的原因,并給出幾種可能的解決方案,需要的朋友可以參考下2024-09-09
詳解django實(shí)現(xiàn)自定義manage命令的擴(kuò)展
這篇文章主要介紹了django實(shí)現(xiàn)自定義manage命令的擴(kuò)展,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
networkx庫(kù)繪制帶權(quán)圖給無權(quán)圖加權(quán)重輸出
這篇文章主要為大家介紹了Python?networkx庫(kù)繪制帶權(quán)圖給無權(quán)圖加權(quán)重并輸出權(quán)重的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python中的hashlib和base64加密模塊使用實(shí)例
這篇文章主要介紹了python中的hashlib和base64加密模塊使用實(shí)例,hashlib模塊支持的加密算法有md5 sha1 sha224 sha256 sha384 sha512,需要的朋友可以參考下2014-09-09
Python控制臺(tái)輸出俄羅斯方塊移動(dòng)和旋轉(zhuǎn)功能
這篇文章主要介紹了Python控制臺(tái)輸出俄羅斯方塊移動(dòng)和旋轉(zhuǎn)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

