回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<pre><code># 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()
</code></pre>
<p>就目前的情况而言,当我输入任何键时,我都会被带进while循环,当它应该把我踢出时。<strong>这是错误的。</strong>我希望这样,只有“Y”</em>才能将我输入while循环,并且当按下“enter”键时,程序将<strong>终止</strong>进入<code>>>></code>。你知道吗</p>
<p>我怎样才能解决这个问题?你知道吗</p>
<p>我做了一些研究,被告知要使用双引号方法将“Enter”键踢出while循环,但正如您所看到的,它在我的代码中不起作用。你知道吗</p>