此程序用于计算贷款的每月还款额。我不断收到该语句的无效语法错误:返回monthly_payment。我计算出一个错误。此外,利率在计算和格式化结果时都应只保留小数点后一位。
enter code here#!/usr/bin/env python3
#
# Monthly Payment Calculator
#
from decimal import Decimal
import locale as lc
def get_month_payment(Loan_amount,Yearly_interest_rate,Years):
monthly_interest_rate = Yearly_interest_rate/12/100
months = Years*12
monthly_payment = Decimal("0.00")
for i in range(months):
monthly_payment += Loan_amount
month_payment = Loan_amount*monthly_interst_rate/ (1-1/(
1 + monthly_interesr_rate)**months)
monthly_payment += month_payment
monthly_payment = monthly_payment.quantize(Decimal("1.00")
return monthly_payment
def main():
print("Monthly Payment Calculator")
print()
print ("DATA ENTRY")
choice = "y"
while choice.lower() == "y":
principle = Decimal(input("Loan amount: "))
annual_interest_rate = Decimal(input("Yearly interest rate: "))
duration=int(input("Years: "))
month_payment = get_month_payment(
Loan_amount,Yearly_interest_rate,Years)
print()
result = lc.setlocale(lc.LC_ALL, "")
if result == "C":
lc.setlocale(lc.LC_ALL, "en_US")
line = "{:16} {:>16}"
print(line.format("Loan amount:",
lc.currency(Loan_amount, grouping=True)))
print(line.format("Yearly interest rate:",
str(interest_rate) + "%"))
print(line.format("Month payment:",
lc.currency(monthly_payment, grouping=True)), "\n")
choice = input("Continue? (y/n): ")
print()
print("Good Bye!")
if __name__ == "__main__":
main()