【问题标题】:Can't exit while-loop with "Enter" key; need only "Y" to enter me into while-loop不能用“Enter”键退出while循环;只需要“Y”就可以让我进入while循环
【发布时间】:2016-11-30 15:54:17
【问题描述】:
# 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 循环,但正如您所见,它不像我的代码那样工作。

【问题讨论】:

    标签: python while-loop


    【解决方案1】:

    你目前把print语句的值作为loan_change的值:

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

    这是目前问题的主要原因,你不需要打印语句,因为打印函数的值是None,所以你的while循环条件稍后会失败,因为None != ""将评估@ 987654324@ 总是。

    相反,只需使用它来获取loan_change 的值:

    loan_change = input("Do you..")
    

    注意input函数,如果里面提供了一个字符串会在询问的时候自动打印出来,所以根本不需要打印:

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

    另外,您应该将 while 循环条件更改为

    while loan_change == "Y":
    

    这将确保您只有在输入 Y 时才进入循环,并且任何其他字符串都将退出/跳过循环。

    【讨论】:

      【解决方案2】:

      在这一行:

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

      【讨论】:

        猜你喜欢
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        • 2015-03-08
        相关资源
        最近更新 更多