Python 簡単な銀行システム

Document 对象参考手册Python3 サンプル

以下のサンプルはPython銀行システムの操作を学習するためのものです:

サンプル

means = [0, 0, 0]  
loan = 0
rate = 0
pay = 0
investment = 0
annual_rate = 0


# 積立投資の予想収益を計算
# 積立投資収益の計算式:M=a(1+x)[-1+(1+x)^n]/x;
# Mは予想収益、aは毎期の積立投資金額、xは収益率、nは積立投資の期数を表します。
# ユーザーが毎月300元を積立投資し、1年で3600元、年間収益率15%と仮定します。
# 積立投資期間を35年とすると、収益は3600(1+15%)[-1+(1+15%)^35]/15%=3648044元と計算できます。
def fixed_investment(inv, a_rate, y):
    global means
    inv = 12 * inv
    a_rate = a_rate / 100
    if a_rate == 0:
        expected = 0
    else:
        expected = inv * (1 + a_rate) * (pow((1 + a_rate), y) - 1) / a_rate
    print("積立投資の予想収益: %.2f" % expected)
    means[1] = expected
    return expected


def balance():
    total = 0
    for i in means:
        total += i
    print("あなたの資産総額:%.2f" % total)
    print("あなたの資産明細:\n")
    print("預金:%.2f" % means[0])
    print("資産運用:%.2f" % means[1])
    print("負債:%.2f" % means[2])


def saving(amount):
    global means
    if amount < 0:
        print("預入金額は0未満にできません!")
    else:
        means[0] += amount
        print("預入済み:%.2f 元" % amount)
        print("現在の残高:%.2f 元" % means[0])


def draw_money(drawing):
    global means
    if drawing < 0:
        print("引出金額は0未満にできません!")
    elif drawing > means[0]:
        print("引出金額は残高を超えられません!")
    else:
        means[0] -= drawing
        print("引出済み: %.2f 元" % drawing)
        print(現在の残高: %.2f 元 % means[0])


def loans(loan, rate, pay, years):
    global means
    if pay < (loan - pay) * rate:
        print("あなたは完済できません!!!")
    else:
        if years == 0:
            count = 0
            while loan > 0:
                loan -= pay
                loan *= (1 + rate)
                count += 1
            print("%d 年後にローンを完済します。" % count)
        else:
            for _ in range(years):
                loan -= pay
                if loan == 0:
                    break
                else:
                    loan *= (1 + rate)
                    print("あなたの現在の負債は: %.2f" % loan)
            # means[2] = loan
            return loan


# 将来の財務状況
def future(years):
    income = fixed_investment(investment, annual_rate, years)
    debt = loans(loan, rate, pay, years)
    captial = means[0] + income - debt
    print("あなたの%i年目の総資産: %.3f" % (years, captial))


def init():
    print()
    print('''以下が取扱業務です:
1. 資産照会
2. 預金
3. 引出し
4. 複利計算
5. ローン計算
6. 将来資産の計算
q. 終了'''
)


def main():
    init()
    while True:
        choice = input("ご利用になる業務コードを入力してください: ")
        # 残高照会
        if choice == "1":
            balance()
        # 預金
        elif choice == "2":
            inc = float(input("預入金額を入力してください: "))
            saving(inc)
        # 引出し
        elif choice == "3":
            dec = float(input("引出金額を入力してください: "))
            draw_money(dec)
        # 積立投資の計算
        elif choice == "4":
            investment = float(input("毎月の積立投資金額を入力してください: "))
            annual_rate = float(input("年間収益率を入力してください: "))
            years = int(input("積立投資期間(年)を入力してください: "))
            if investment <= 0 or annual_rate <= 0 or years <= 0:
                print("入力データが正しくありません")
            else:
                money = fixed_investment(investment, annual_rate, years)
            print("最終収益: %.2f 元" % money)
        # ローンの計算
        elif choice == "5":
            loan = float(input("現在のローン額を入力してください: "))
            rate = float(input("年間金利を入力してください: "))
            pay = float(input("毎年の返済額を入力してください: "))
            if loan <= 0 or rate <= 0 or pay <= 0:
                print("入力データが正しくありません")
            else:
                loans(loan, rate, pay, 0)
        elif choice == "6":
            years = int(input("何年後の財務状況を照会しますか? "))
            future(years)
        # 終了
        elif choice == "q":
            print("またのご利用をお待ちしております!さようなら!")
            break
        else:
            print("入力された指令が正しくありません。再入力してください\n")


if __name__ == '__main__':
    main()

Document 对象参考手册Python3 サンプル

その他の拡張