基于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)文章
Django url,從一個(gè)頁面調(diào)到另個(gè)頁面的方法
今天小編就為大家分享一篇Django url,從一個(gè)頁面調(diào)到另個(gè)頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08關(guān)于Python常用函數(shù)中NumPy的使用
這篇文章主要介紹了關(guān)于Python常用函數(shù)中NumPy的使用,在Python中有很多常用的函數(shù),NumPy就是其中之一,那么NumPy該怎么使用,下面就一起來看看吧2023-03-03Python如何通過subprocess調(diào)用adb命令詳解
python可以說是寫一些小腳本的利器語法簡(jiǎn)單,做為最著名的就“膠水語言”用它來寫一些命令腳本非常的方便。下面這篇文章主要給大家介紹了關(guān)于Python如何通過subprocess調(diào)用adb命令的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08