# 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()
As it stands, when I enter any key I am taken inside the while-loop when it should be kicking me out . This is wrong. I would like it so that only "Y" can enter me into the while-loop and that when the "Enter" key is struck, the program is terminated into >>>
.
How can I solve this?
I've done some research and was told to use the double quotation method to have the "Enter" key kick out of while-loop, but as you can see it is not working as it stands in my code.
You currently take the value of the print statement as the value of loan_change:
loan_change=print(input("Do you.."))
This is the main reason of the problem currently, you don't need a print statement there, as the value of the print function is None
, so your while loop condition fails later, as None != ""
will evaluate True
always.
Instead, simply use this to get the value of loan_change
:
loan_change = input("Do you..")
Note that the input
function, if provided a string inside will automatically print it while asking, so the print is not needed at all:
>>> input("Enter some value: ")
Enter some value: 123
'123'
Also, You should change your while loop condition to
while loan_change == "Y":
This will ensure that you enter the loop only when you have an entered a Y, and any other string will exit/skip the loop.
On this line:
loan_change=print(input("Do you want to change the loan amount? Y for Yes OR Enter to Quit: "))
You print the return value of the input
function. Then you assign the return value of print
to loan_change
. Since print
doesn't return anything, loan_change
will be of type NoneType
.
Next, you check whether loan_change
is not equal to ""
. Since loan_change
has a completely different type from ""
, they will not be equal. The condition is satisfied, and the while loop executes.
To fix it, change your code to this:
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: ")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.