[Python] A small case of mortgage calculator function

Topic requirements:

The mortgage calculation formula is as follows:
〉Monthly monthly payment reference = loan amount × [monthly interest rate × (1+monthly interest rate)^number of repayment months]÷{[(1+monthly interest rate)^number of repayment months]–1}>payment Total payment = reference monthly monthly payment × term × 12
≥ Interest payment = total repayment – ​​loan amount × 10,000.
The monthly interest rate in the above calculation method (monthly interest rate = interest rate ÷ 12)) refers to the interest calculated on a monthly basis. The interest rates for different loan types are different: for commercial loans, the interest rate for loans under five years (including five years) is 4.75%, and for loans over five years, the interest rate is 4.90%; for provident fund loans, the interest rate for loans under five years is 4.75%. The loan interest rate (including five years) is 2.75%, and the interest rate for more than five years is 3.25%.
This case requires writing a program to develop a mortgage calculator based on the above calculation method.


Complete code:

#核心计算公式
def payment_loan(interest_rate,loan_amount,loan_period):
    monthly_interest_rate = interest_rate / 12
    num_payments = loan_period * 12

    monthly_payment = loan_amount * 10000 * (monthly_interest_rate * (1 + monthly_interest_rate) ** num_payments) / (
                ((1 + monthly_interest_rate) ** num_payments) - 1)
    total_payment = monthly_payment * num_payments
    interest_payment = total_payment - loan_amount * 10000
    return{ "monthly_payment":monthly_payment,
            "interest_payment":interest_payment,
            "total_payment":total_payment}
#主函数
def calculate_loan():
    while True:
        payment={};
        loan_type = input("请选择贷款类型:1.商业贷款 2.公积金贷款 3.组合贷款 0.退出:")
        if loan_type=="0":
            break;
        if loan_type == "1":
            #商业贷款计算
            loan_amount = float(input("请输入贷款金额(万):"))
            loan_period = int(input("请输入贷款期限(年)5 10 15 20 25 30:"))
            interest_rate = 0.0475
            if loan_period > 5:
                interest_rate = 0.049
            payment=payment_loan(interest_rate, loan_amount, loan_period)
        elif loan_type == "2":
            #公积金贷款计算
            loan_amount = float(input("请输入贷款金额(万):"))
            loan_period = int(input("请输入贷款期限(年)5 10 15 20 25 30:"))
            interest_rate = 0.0275
            if loan_period > 5:
                interest_rate = 0.0325
            payment=payment_loan(interest_rate, loan_amount, loan_period)
        elif loan_type == "3":
            # 混合贷款计算
            loan_amount1 = float(input("请输入商业贷款金额(万):"))
            loan_amount2 = float(input("请输入公积金贷款金额(万):"))
            loan_period = int(input("请输入贷款期限(年)5 10 15 20 25 30:"))
            interest_rate = 0.0475
            if loan_period > 5:
                interest_rate = 0.049
            payment1 = payment_loan(interest_rate, loan_amount1, loan_period)
            interest_rate = 0.0275
            if loan_period > 5:
                interest_rate = 0.0325
            payment2=payment_loan(interest_rate, loan_amount2, loan_period)
            for key in payment1:
                payment[key]=payment1[key]+payment2[key]
        else:
            print("无效的贷款类型。")
            return
        #打印最终结果
        print("每月月供参考:{:.2f} 元".format(payment["monthly_payment"]))
        print("支付利息:{:.2f} 元".format(payment["interest_payment"]))
        print("还款总额:{:.2f} 元".format(payment["total_payment"]))
calculate_loan()

Implementation ideas:

This code implements the function of a mortgage calculator. Mainly includes the following functions:

  1. payment_loan(interest_rate, loan_amount, loan_period)Functions used to calculate the monthly payment, interest payments, and total repayment of a loan. It accepts the interest rate (interest_rate), loan amount (loan_amount) and loan period (loan_period) as parameters and returns a dictionary containing the monthly payment, interest paid and total repayment.

  2. calculate_loan()The function is the main function, which allows the user to select the loan type and enter the corresponding information through a loop, and then calls payment_loan()the function to calculate the corresponding loan information. According to different loan types, different interest rates and loan amounts are selected, and finally the calculation results are displayed to the user.

  • If the selected loan type is "1" (commercial loan) or "2" (provident fund loan), the user is required to enter the loan amount and loan term, and set the corresponding interest rate according to the loan term. The function is then called payment_loan()to calculate the loan information and the results are stored in paymentthe dictionary.

  • If the loan type is selected as "3" (combination loan), the user is required to enter the commercial loan amount and provident fund loan amount respectively, as well as the loan term. Set the corresponding interest rate according to the loan term, call payment_loan()the function to calculate the loan information of commercial loans and provident fund loans, and store the results in the payment1and payment2dictionary. Then, the values ​​for the corresponding keys in the two dictionaries are added and the combined result is stored in paymentthe dictionary.

Finally, based on the calculated loan information, the monthly monthly payment, interest payment and total repayment are output to the user using formatted strings.

actual effect:

 

 

 

Guess you like

Origin blog.csdn.net/zhangawei123/article/details/130927869