贷款计算器程序

时间:2017-02-19 18:27:51

标签: python

到目前为止,我整个星期都在编写这个版本,试图让它发挥作用。

它应该如下:

Please enter Amount that you would like to borrow(£): 4000
Please enter Duration of the loan(Years):2
Please enter the interest rate (%):6
The total amount of interest for 2 (years) is: £480.00
The total amount of the period for 2 (years) is £4480.00
You will pay £186.67 per month for 24 months.
Do you wish to calculate a new loan payment(Y or N)

代码:

monthlypayment = 0  #Variable
loanamount = 0 #Variable
interestrate = 0 #Variable
numberofpayments    = 0   #Variable
loandurationinyears = 0 #Variable

loanamount = input("Please enter the amount that you would like to borrow(£) ")
loandurationinyears = input("How many years will it take you to pay off the loan? ")
interestrate = input("What is the interest rate on the loan? ")

#Convert the strings into floating numbers
loandurationinyears = float(loandurationinyears)
loanamount = float(loanamount)
interestrate = float(interestrate)

#Since payments are once per month, number of payments is number of years for the loan
numberofpayments = loandurationinyears*12

#calculate the monthly payment based on the formula
monthlypayment = (loanamount * interestrate * (1+ interestrate)
                    * numberofpayments / ((1 + interestrate) * numberofpayments -1))

#Result to the program
print("Your monthly payment will be " + str(monthlypayment))

1 个答案:

答案 0 :(得分:0)

在我看来,就像你所描述的那样,你在上面的代码中唯一缺少的是while循环。这将允许您的程序计算贷款并反复运行程序,直到用户输入no,在这种情况下程序退出。所有你需要做的是:

YorNo = input("Do you wish to calculate a loan payment")
YorNo.Title()

while YorNo != "n":


    #Your code goes here


    YorNo = input("Do you wish to calculate a loan payment")
    YorNo.Title()
print("Thank you for using the program")

如果您不明白这一点,那么您可以在代码之前键入前3行。然后你留下一个缩进并在它们后面键入你的代码。完成后,键入第3行和第4行。然后,简单地返回缩进(以显示程序这不是循环的一部分。)如果我没有弄错,结果将是:

  1. 您会被问到是否想要计算贷款
  2. 如果你回答" y"您的代码将运行,贷款将被计算并打印给用户
  3. 然后你会再被问到。以上内容将重复,直到您输入" n" N不能大写