python語言學習筆記(一)------匯率兌換
阿新 • • 發佈:2019-01-10
根據輸入判斷是人民幣還是美元,進行相應的轉換計算,將匯率兌換功能封裝到函式中,使結構程式化,函式lambda
def main(): """ 主函式 """ # 匯率 USD_VS_RMB = 6.77 # 帶單位的貨幣輸入 currency_str_value = input('請輸入帶單位的貨幣金額:') unit = currency_str_value[-3:] if unit == 'CNY': exchange_rate = 1 / USD_VS_RMB elif unit == 'USD': exchange_rate = USD_VS_RMB else: exchange_rate = -1 if exchange_rate != -1: in_money = eval(currency_str_value[:-3]) # 使用lambda定義函式 convert_currency2 = lambda x: x * exchange_rate # # 呼叫函式 # out_money = convert_currency(in_money, exchange_rate) # 呼叫lambda函式 out_money = convert_currency2(in_money) print('轉換後的金額:', out_money) else: print('不支援該種貨幣!') if __name__ == '__main__': main()