2016-11-30 86 views
1
# 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循環,但正如您可以看到它不起作用,因爲它代表我的代碼。

回答

1

您目前採取print語句的值作爲loan_change的價值:

loan_change=print(input("Do you..")) 

這是問題的主要原因,目前,你並不需要一個print語句在那裏,作爲價值打印功能是None,因此您的while循環條件稍後會失敗,因爲None != ""將始終評估True

而是直接使用該得到的loan_change值:

loan_change = input("Do you..") 

注意,input功能,如果提供的字符串中會自動打印一邊問,所以不需要在所有的打印:

>>> input("Enter some value: ") 
Enter some value: 123 
'123' 

此外,你應該改變你的while循環條件

while loan_change == "Y": 

這將確保您只有在輸入Y時才進入循環,而其他任何字符串都將退出/跳過循環。

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: ")