# 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循环,但正如您所看到的,它在我的代码中不起作用。你知道吗
在这条线上:
打印
input
函数的返回值。然后将返回值print
赋给loan_change
。因为print
不返回任何内容,loan_change
将是NoneType
类型。你知道吗接下来,检查
loan_change
是否不等于""
。因为loan_change
与""
的类型完全不同,所以它们不相等。满足条件,执行while循环。你知道吗要修复它,请将代码更改为:
您当前将print语句的值作为loan\u change的值:
这是当前问题的主要原因,您不需要print语句,因为print函数的值是
None
,所以while循环条件稍后会失败,因为None != ""
将始终计算True
。你知道吗相反,只需使用它来获得
loan_change
的值:请注意,
input
函数(如果在函数中提供字符串)将在请求时自动打印它,因此根本不需要打印:另外,您应该将while循环条件更改为
这将确保只有在输入Y时才进入循环,并且任何其他字符串都将退出/跳过循环。你知道吗
相关问题 更多 >
编程相关推荐