標籤:python
import recontent = {‘T‘:‘溫度轉換‘, ‘L‘:‘長度轉換‘, ‘D‘:‘貨幣轉換‘ }for k,v in content.items(): print(k,v)switch_type = input(‘請輸入轉換類型:‘)if switch_type == ‘T‘: temp = input(‘請輸入溫度(樣本:1C或者1F):‘) if re.search(‘C‘,temp): temp = float(temp.strip(‘C‘)) Tf = (9/5)*temp + 32 print(f‘F=9/5*{temp}+32={Tf}F‘) elif re.search(‘F‘,temp): temp = float(temp.strip(‘F‘)) Tc = (5/9)*(temp - 32) print(f‘C=5/9*({temp}-32)={Tc}C‘) else: print(‘輸入有誤,樣本:1C或者1F‘)if switch_type == ‘L‘: temp = input(‘請輸入長度(樣本:1m或者1ft):‘) if re.search(‘m‘,temp): temp = float(temp.strip(‘m‘)) ft = 3.281*temp print(f‘ft=3.281*{temp}={ft}ft‘) elif re.search(‘ft‘,temp): temp = float(temp.strip(‘ft‘)) M = 0.3048*temp print(f‘M=3.281*{temp}={M}m‘) else: print(‘輸入有誤,樣本:1m或者1ft‘) if switch_type == ‘D‘: temp = input(‘請輸入貨幣(樣本:1美元或者1人民幣元):‘) if re.search(‘美元‘,temp): temp = float(temp.strip(‘美元‘)) RMB = 6.671*temp print(f‘RMB=6.671*{temp}={RMB}人民幣元‘) elif re.search(‘人民幣元‘,temp): temp = float(temp.strip(‘人民幣元‘)) dollar = 0.1499*temp print(f‘dollar=0.1499*{temp}={dollar}美元‘) else: print(‘輸入有誤,樣本:1美元或者1人民幣元‘)
T 溫度轉換L 長度轉換D 貨幣轉換請輸入轉換類型:T請輸入溫度(樣本:1C或者1F):10CF=9/5*10.0+32=50.0F
案例:python實現單位轉換