python中溫度單位轉(zhuǎn)換的實(shí)例方法
溫度有攝氏度和華氏度兩個(gè)單位,我們通常使用的是攝氏度,對(duì)于轉(zhuǎn)換成華氏度,很多小伙伴記不住公式。作為萬(wàn)能的計(jì)算機(jī),它是可以幫助我們解決溫度單位轉(zhuǎn)換的問題。本文主要演示python中進(jìn)行溫度單位轉(zhuǎn)換的代碼過程,具體請(qǐng)看本文。
一、問題
溫度有攝氏度(Celsius)和華氏度(Fabrenheit)兩個(gè)不同的單位。攝氏度0度為結(jié)冰點(diǎn),沸點(diǎn)為100度;華氏度以32度為冰點(diǎn),以212度為沸點(diǎn)。一般來(lái)說(shuō),中國(guó)采用攝氏度,美國(guó)采用華氏度。
兩者之間的轉(zhuǎn)換公式為:攝氏度=(華氏度-32)/1.8、華氏度=攝氏度*1.8+32。
二、代碼
輸入
#定義一個(gè)函數(shù)獲取帶符號(hào)的溫度值。
def tempstr():
while True:
temp=input('請(qǐng)輸入帶有符號(hào)[C代表攝氏度,F(xiàn)代表華氏度]的溫度數(shù)值:')
if temp[-1] in ['c','C','f','F']:
return temp
else: #如果輸入的溫度值沒有帶有符號(hào),會(huì)提示輸入錯(cuò)誤并被要求重新輸入。
print('輸入錯(cuò)誤,請(qǐng)輸入帶有符號(hào)的溫度數(shù)值')
print('-'*20)
處理輸出
#定義一個(gè)函數(shù)獲取帶符號(hào)的溫度值。
def tempstr():
while True:
temp=input('請(qǐng)輸入帶有符號(hào)[C代表攝氏度,F(xiàn)代表華氏度]的溫度數(shù)值:')
if temp[-1] in ['c','C','f','F']:
return temp
else: #如果輸入的溫度值沒有帶有符號(hào),會(huì)提示輸入錯(cuò)誤并被要求重新輸入。
print('輸入錯(cuò)誤,請(qǐng)輸入帶有符號(hào)的溫度數(shù)值')
print('-'*20)
總體代碼
def tempstr():
while True:
temp=input('請(qǐng)輸入帶有符號(hào)[C代表攝氏度,F(xiàn)代表華氏度]的溫度數(shù)值:')
if temp[-1] in ['c','C','f','F']:
return temp
else:
print('輸入錯(cuò)誤,請(qǐng)輸入帶有符號(hào)的溫度數(shù)值')
print('-'*20)
def progress(temp):
if temp[-1] in ['F','f']:
output=(eval(temp[:-1])-32)/1.8
print('溫度轉(zhuǎn)換為攝氏度為{:.2f}C'.format(output))
else:
output=eval(temp[:-1])*1.8+32
print('溫度轉(zhuǎn)換為華氏度為{:.2f}F'.format(output))
temp=tempstr()
progress(temp)
溫度單位轉(zhuǎn)換實(shí)例擴(kuò)展:
module:temp
def temp_f_to_c(f): return (f - 32) * (5 / 9) def temp_c_to_f(c): return (c * 9 / 5) + 32 def 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()
到此這篇關(guān)于python中溫度單位轉(zhuǎn)換的實(shí)例方法的文章就介紹到這了,更多相關(guān)python中溫度單位如何轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)調(diào)用攝像頭拍攝照片
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)調(diào)用攝像頭拍攝照片以及郵箱收集功能,文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手嘗試一下2022-06-06
Python常見庫(kù)matplotlib學(xué)習(xí)筆記之畫圖文字的中文顯示
在Python中使用matplotlib或者plotnine模塊繪圖時(shí),常常出現(xiàn)圖表中無(wú)法正常顯示中文的問題,下面這篇文章主要給大家介紹了關(guān)于Python常見庫(kù)matplotlib學(xué)習(xí)筆記之畫圖文字的中文顯示的相關(guān)資料,需要的朋友可以參考下2023-05-05
python eval 轉(zhuǎn)換k m到乘法計(jì)算的操作
這篇文章主要介紹了python eval 轉(zhuǎn)換k m到乘法計(jì)算的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python動(dòng)態(tài)規(guī)劃算法實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于python動(dòng)態(tài)規(guī)劃算法實(shí)例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2020-11-11
Django Haystack 全文檢索與關(guān)鍵詞高亮的實(shí)現(xiàn)
這篇文章主要介紹了Django Haystack 全文檢索與關(guān)鍵詞高亮的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
使用Python實(shí)現(xiàn)將PDF轉(zhuǎn)為PDF/A和PDF/X
PDF/A和PDF/X是兩種有特定用途的PDF格式,本文主要介紹了如何使用Python將PDF轉(zhuǎn)換為PDF/A和PDF/X,以及如何將PDF/A格式轉(zhuǎn)換回標(biāo)準(zhǔn)的PDF格式,需要的可以參考下2024-04-04

