見出し画像

[Python]消費税計算をクロージャーで表現

1.クロージャーで消費税計算

消費税計算をクロージャーを用いて表現します。

2.コード

def calc_price_tax(tax):
    def calc_price(price):
        return int((1 + (tax / 100)) * price)
    return calc_price

if __name__ == "__main__":
    #消費税8%
    calc_price_tax_8per = calc_price_tax(8)
    #消費税5%
    calc_price_tax_5per = calc_price_tax(5)
    
    price_list = [100, 300, 500, 900]
    #それぞれの税率で計算した税込価格
    for price in price_list:
        print("8%", calc_price_tax_8per(price), "円")
        print("5%", calc_price_tax_5per(price), "円")
   

3.実行結果

8% 108 円
5% 105 円
8% 324 円
5% 315 円
8% 540 円
5% 525 円
8% 972 円
5% 945 円



この記事が気に入ったらサポートをしてみませんか?