python匯率兌換


eval()函數:將字符串str當成有效的表達式來求值並返回計算結果。

if __name__ == '__main__'的意思是:當.py文件被直接運行時,if __name__ == '__main__'之下的代碼塊將被運行;當.py文件以模塊形式被導入時,if __name__ == '__main__'之下的代碼塊不被運行。

 

def main():
    exchange_rate = 6.77  # 匯率
    currency_str_value = input("請輸入帶單位的貨幣金額(CNY/USD):")

    unit = currency_str_value[-3:]

    if unit == "CNY":
        number = 1 / exchange_rate
    elif unit == "USD":
        number = exchange_rate
    else:
        number = -1

    if number != -1:
        in_money = eval(currency_str_value[:-3])
        convert_currency2 = lambda x: x * number
        out_money = convert_currency2(in_money)
        print("轉換后的金額: ", out_money)
    else:
        print("目前版本尚不支持該種貨幣")

if __name__ == '__main__':
    main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM