国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python中溫度單位轉換的實例方法

瀏覽:155日期:2022-07-01 10:00:09

溫度有攝氏度和華氏度兩個單位,我們通常使用的是攝氏度,對于轉換成華氏度,很多小伙伴記不住公式。作為萬能的計算機,它是可以幫助我們解決溫度單位轉換的問題。本文主要演示python中進行溫度單位轉換的代碼過程,具體請看本文。

一、問題

溫度有攝氏度(Celsius)和華氏度(Fabrenheit)兩個不同的單位。攝氏度0度為結冰點,沸點為100度;華氏度以32度為冰點,以212度為沸點。一般來說,中國采用攝氏度,美國采用華氏度。

兩者之間的轉換公式為:攝氏度=(華氏度-32)/1.8、華氏度=攝氏度*1.8+32。

二、代碼

輸入

#定義一個函數獲取帶符號的溫度值。def tempstr(): while True: temp=input(’請輸入帶有符號[C代表攝氏度,F代表華氏度]的溫度數值:’) if temp[-1] in [’c’,’C’,’f’,’F’]: return temp else: #如果輸入的溫度值沒有帶有符號,會提示輸入錯誤并被要求重新輸入。 print(’輸入錯誤,請輸入帶有符號的溫度數值’) print(’-’*20)

處理輸出

#定義一個函數獲取帶符號的溫度值。def tempstr(): while True: temp=input(’請輸入帶有符號[C代表攝氏度,F代表華氏度]的溫度數值:’) if temp[-1] in [’c’,’C’,’f’,’F’]: return temp else: #如果輸入的溫度值沒有帶有符號,會提示輸入錯誤并被要求重新輸入。 print(’輸入錯誤,請輸入帶有符號的溫度數值’) print(’-’*20)

總體代碼

def tempstr(): while True: temp=input(’請輸入帶有符號[C代表攝氏度,F代表華氏度]的溫度數值:’) if temp[-1] in [’c’,’C’,’f’,’F’]: return temp else: print(’輸入錯誤,請輸入帶有符號的溫度數值’) print(’-’*20)def progress(temp): if temp[-1] in [’F’,’f’]: output=(eval(temp[:-1])-32)/1.8 print(’溫度轉換為攝氏度為{:.2f}C’.format(output)) else: output=eval(temp[:-1])*1.8+32 print(’溫度轉換為華氏度為{:.2f}F’.format(output))temp=tempstr()progress(temp)

溫度單位轉換實例擴展:

module:temp

def temp_f_to_c(f): return (f - 32) * (5 / 9)def temp_c_to_f(c): return (c * 9 / 5) + 32def main(): print(temp_c_to_f(100))if __name__ == ’__main__’: main()

main function:

import temps def convert_temp_system(temp, temp_system): if temp_system == ’c’: new_temp = temps.temp_c_to_f(temp) else: new_temp = temps.temp_f_to_c(temp) return new_temp def print_temp_message(original_temp, new_temp, system): if system == ’f’: print(original_temp, ’degrees F converted to C is ’, new_temp) else: print(original_temp, ’degrees C converted to F is ’, new_temp) def main(): temp = float(input(’Enter the temperature: ’)) system = input(’F or C: ’) converted_temp = convert_temp_system(temp, system) print_temp_message(temp, converted_temp, system) if __name__ == ’__main__’: main()

到此這篇關于python中溫度單位轉換的實例方法的文章就介紹到這了,更多相關python中溫度單位如何轉換內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 柳河县| 东明县| 哈尔滨市| 丰顺县| 台前县| 庆元县| 新营市| 山丹县| 兰州市| 遵义市| 长岛县| 确山县| 来宾市| 进贤县| 深州市| 沐川县| 万年县| 新疆| 山西省| 许昌市| 耒阳市| 新营市| 左贡县| 青田县| 民权县| 布拖县| 松潘县| 通辽市| 交城县| 平江县| 红桥区| 大宁县| 汽车| 永吉县| 苍梧县| 萍乡市| 德庆县| 郓城县| 陇西县| 朝阳市| 定日县|