2017-02-19 109 views
0

到目前为止,我一直在编码这一整个星期试图让它工作。贷款计算器程序

应该站出来,因为这:

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

那么会出现什么样的错误?此外,你应该可能将你的利率输入除以100. – ryugie

+0

它基本上会是一个模糊的数量,如生病6,2,6,它会拿出类似6056235.14291821421542 –

+0

欢迎来到堆栈溢出!我编辑你的问题来修复语法并使其更易读。另外,我更正了想要的输出中的推测错字('&' - >'£')。请[编辑]您的问题以包含完整的实际输出或发布的代码,以便我们有[mcve]。 –

回答

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

如果你不明白这一点,baisically,你只是你的代码之前输入第3行。然后你留下一个缩进,并在他们后面输入你的代码。一旦完成,您可以输入第3行和第4行。然后,只需回头是缩进(显示一个程序,这个心不是循环的一部分。)如果我不是错了,这样做的结果将是:

  1. 你会被要求无论您要计算贷款
  2. 如果你回答“y”,你的代码将运行,贷款将被计算并打印给用户
  3. 然后你会再次被问到。以上将重复,直到您输入“n” N不能被大写
+0

当我做这个等式时,它总共提供了大约2555+的金额 –