2017-02-19 181 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+的金額 –