汇率兑换4。0

敲黑板!!!!!!

1、定义函数

 def convert_currency(im, er):       #使用 <def 函数名(参数):> + <函数体> 来定义函数
     """
         汇率兑换函数
     """
     out = im * er
     return out

2、变量赋值

    USD_VS_RMB = 6.77            

  

    currency_str_value = input('请输入带单位的货币金额:')    #使用 input 接收键盘输入的字符串

 

3、字符串操作

  unit = currency_str_value[-3:]      #将从位置-3开始到最后结束的字符串(后三位字符串)赋值给 unit

4、判断分支结构

if unit == 'CNY':            # 判断输入字符后三位是否为CNY,从而判断输入货币类型
  exchange_rate = 1 / USD_VS_RMB
elif unit == 'USD':
  exchange_rate = USD_VS_RMB
else:
  exchange_rate = -1

5、lambda函数使用

主要用于,函数体表达式可以一行写完的函数

if exchange_rate != -1:
        in_money = eval(currency_str_value[:-3])
        # 使用lambda定义函数
        convert_currency2 = lambda x: x * exchange_rate
        # 调用lambda函数
        out_money = convert_currency2(in_money)
        print('转换后的金额:', out_money)
    else:
        print('不支持该种货币!')

6、主函数调用

if __name__ == '__main__':   #请参考(https://blog.csdn.net/heqiang525/article/details/89879056)
    main()