# Create a class called "Loan":
# Data fields in the Loan class include: Annual Interest Rate(Float),\
# Number of years of loan(Float), Loan Amount(Float), and Borrower's Name(string)
class Loan:
# Create the initializer or constructor for the class with the above data fields.
# Make the data fields private.
def __init__(self, annualInterestRate, numberOfYears, loanAmount, borrowerName):
self.__annualInterestRate=annualInterestRate
self.__numberOfYears=numberOfYears
self.__loanAmount=loanAmount
self.__borrowerName=borrowerName
self.monthlyPayment__ = None
self.totalPayment__ = None
# Create accessors (getter) for all the data fields:
def getannualInterestRate(self):
return self.__annualInterestRate
def getnumberOfYears(self):
return self.__numberOfYears
def getloanAmount(self):
return self.__loanAmount
def getborrowerName(self):
return self.__borrowerName
# Create mutators (setters) for all the data fields:
def setannualInterestRate(self):
self.__annualInterestRate=annualInterestRate
def setnumberOfYears(self):
self.__numberOfYears=numberOfYears
def setloanAmount(self):
self.__loanAmount=loanAmount
def setloanAmount(self,loan2):
self.__loanAmount=loan2
def setborrowerName(self):
self.borrowerName=borrowerName
# Create a class method: getMonthlyPayment -
def getMonthlyPayment(self,loanAmount, monthlyInterestRate, numberOfYears):
monthlyPayment = loanAmount * monthlyInterestRate/(1- \
(1/(1 + monthlyInterestRate) ** (numberOfYears * 12)))
return monthlyPayment
# Create a class method: getTotalPayment -
def getTotalPayment(self):
monthlyPayment = self.getMonthlyPayment(float(self.getloanAmount()),
float(self.getannualInterestRate())/1200,
int(self.getnumberOfYears()))
self.monthlyPayment__=monthlyPayment
totalPayment =self.monthlyPayment__ * 12 \
* int(self.getnumberOfYears())
self.totalPayment__=totalPayment
return self.totalPayment__
# Write a test program (main function) to allow the user to enter the following:
def main():
loan1=Loan(float(input(("Enter yearly interest rate, for exmaple, 7.25: "))),\
float(input(("Enter number of years as an integer: "))),\
float(input(("Enter loan amount, for example, 120000.95: "))),\
input(("Enter a borrower's name: ")))
print()
print("The loan is for", loan1.getborrowerName())
print("The monthly payment is", format(loan1.getMonthlyPayment(loan1.getloanAmount(), \
(loan1.getannualInterestRate()/1200), loan1.getnumberOfYears()), '.2f'))
print("The total payment is", format(loan1.getTotalPayment(), '.2f'))
print()
loan_change=print(input("Do you want to change the loan amount? Y for Yes OR Enter to Quit: "))
while loan_change!="":
print()
loan2=float(input("Enter a new loan amount: "))
loan1.setloanAmount(loan2)
print("The loan is for", loan1.getborrowerName())
print("The monthly payment is", format(loan1.getMonthlyPayment(loan1.getloanAmount(), \
(loan1.getannualInterestRate()/1200), loan1.getnumberOfYears()), '.2f'))
print("The total payment is", format(loan1.getTotalPayment(), '.2f'))
print()
loan_change=print(input("Do you want to change the loan amount? Y for Yes OR Enter to Quit: "))
main()
目前的情況是,當我輸入密鑰我採取內 while循環時,它應該是踢我出去。 這是錯誤的。我想這樣只有「Y」可以進入while循環,當「Enter」鍵被敲擊時,程序是終止到>>>
。無法用「Enter」鍵退出while循環;只需要「Y」進入我進入while循環
我該如何解決這個問題?
我已經做了一些研究,並被告知使用雙引號方法讓「回車」鍵踢出while循環,但正如您可以看到它不起作用,因爲它代表我的代碼。