python中的贷款还款计算
这是我到目前为止所做的,但我只是对如何添加以下requirements Call函数感到困惑,该函数使用适当的参数计算付款,并输出贷款金额、python中的贷款还款计算,python,Python,这是我到目前为止所做的,但我只是对如何添加以下requirements Call函数感到困惑,该函数使用适当的参数计算付款,并输出贷款金额、年利率、付款数量、付款、已偿还金额和已偿还利息的值 def myMonthlyPayment(Principal, annual_r, years): n = years * 12 # number of monthly payments r = (annual_r / 100) / 12 # decimal monthly intere
年利率、付款数量、付款、已偿还金额和已偿还利息的值
def myMonthlyPayment(Principal, annual_r, years):
n = years * 12 # number of monthly payments
r = (annual_r / 100) / 12 # decimal monthly interest rate from APR
MonthlyPayment = (r * Principal * ((1+r) ** n)) / (((1+r) ** n) - 1)
return MonthlyPayment
years = int(input('Please enter number of years of loan: '))
annual_r = float(input('Please enter the annual interest rate: '))
Principal = int(input('Please enter the amount of loan: '))
print('Monthly payment: {}'.format(myMonthlyPayment(Principal, annual_r,
years)))
一个建议是:变量名和参数名使用小写是惯例;使用大写字母 (Principal和MyMonthlyPayment)意味着它是一个类,可以 可能会误导阅读代码的人 它还会导致语法着色提供不正确的信息 至于你的问题,你已经让用户提供了年利率,所以你可以打印出来 对于其他值,您可以向用户索要,也可以使用 和你到目前为止采取的方法一样 一个可能有用的提示是,您可以将多个参数传递给format(),如下所示:
print('Monthly payment: {}, loan amount: {}, annual rate: {}'.format(monthlyPayment,
loan, annual_r))
我没有验证数学,但是你的代码运行了,方法很好!我收到以下错误:
name错误:未定义名称“m”
#Cristian Benitez
principal = float(input("total loan amount taken: "))
interest_rate = float(input("annual interest rate applied: "))
duration = int(input("loan duration in years: "))
def monthly_loan(principal,interest_rate,duration):
n = duration*12 #total number of months
r = interest_rate/(100*12) #interest per month
monthly_payment = principal*((r*((r+1)**n))/(((r+1)**n)-1)) #formula for
compound interest applied on mothly payments.
return monthly_payment
#remaining loan balance calculation
def remaining_bal(principal,annual_interest_rate,duration,payments):
r = annual_interest_rate/1200 # monthly interest rate
m = r + 1
n = duration*12 #duration in months
# remaining balance using compound interest formula
remaining = principal*(((m**n)-(m**payments))/((m**n)-1))
return remaining
monthly = monthly_loan(principal,interest_rate,duration)
print("Loan amount: ",principal," Interest rate: ",interest_rate)
print("Duration (Years): ",duration," Monthly payment: ",int(monthly))