# 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()
按照目前的情况,当我输入任何键时,我在内部时将踢出去。 这是错误的。我希望只有“Y”可以让我进入while循环,当触发“Enter”键时,程序是终止到>>>
。
我该如何解决这个问题?
我做了一些研究,并被告知要使用双引号方法让“回车”按键跳出while循环,但正如你所看到的那样,它在我的代码中不起作用。
答案 0 :(得分:1)
您目前将print语句的值作为loan_change的值:
loan_change=print(input("Do you.."))
这是当前问题的主要原因,你不需要那里的print语句,因为print函数的值是None
,所以你的while循环条件稍后失败,因为{{1} }将始终评估None != ""
。
相反,只需使用它来获取True
:
loan_change
请注意,loan_change = input("Do you..")
函数(如果在其中提供了一个字符串)会在询问时自动打印,因此根本不需要打印:
input
此外,您应该将while循环条件更改为
>>> input("Enter some value: ")
Enter some value: 123
'123'
这将确保只有在输入Y时才进入循环,任何其他字符串将退出/跳过循环。
答案 1 :(得分:1)
在这一行:
loan_change=print(input("Do you want to change the loan amount? Y for Yes OR Enter to Quit: "))
您打印input
功能的返回值。然后,将print
的返回值指定给loan_change
。由于print
没有返回任何内容,loan_change
的类型为NoneType
。
接下来,检查loan_change
是否不等于""
。由于loan_change
与""
的类型完全不同,因此它们不相等。满足条件,并执行while循环。
要解决此问题,请将代码更改为:
loan_change=input("Do you want to change the loan amount? Y for Yes OR Enter to Quit: ")
while loan_change=="Y":
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=input("Do you want to change the loan amount? Y for Yes OR Enter to Quit: ")