2016-11-25 26 views
0

我試圖編寫一個函數(在Python 2.7中),它需要一個未完成的餘額和年利率,然後使用二分搜索 to solve problem #3返回最小的每月支付到最近的分。我試圖通過在主函數內寫入一個函數來遵循DRY原則,該函數應該在一年後返回一個具有餘額的列表,並且月數(如果餘額達到零或更少,則循環會中斷),這將需要兩次計算在我的主要功能。由於我在繼續之前嘗試測試此初始封閉,因此在分配monthlyPayment的行上出現語法錯誤。我究竟做錯了什麼?試圖編寫Python封閉的語法無效

# Problem Set 1("C") 
# Time Spent: xx hours 

def payInOne_BisectionSearch (balance,annualRate): 
    #initialize variables 
    initialBalance = balance 
    monthlyRate = annualRate/12 
    minMonthly = balance/12 
    maxMonthly = (balance * (1 + monthlyRate ** 12)/12 
    monthlyPayment = (minMonthly + maxMonthly)/2 
    numMonths = 1 
    #define function to check balance after 12 months  
    def balanceAfterYear (balance, monthlyRate, monthlyPayment): 
     for numMonths in range (1,13): 
      interest = balance * monthlyRate 
      balance += interest - monthlyPayment 
      if balance <= 0: 
       break 
     return [balance, numMonths] 
    resultList = balanceAfterYear(initialBalance, monthlyRate, monthlyPayment) 
    print resultList[0],resultList[1] 


payInOne_BisectionSearch (input("Enter the outstanding balance"),input("Enter annual rate as a decimal")) 

回答

1

您忘記了上一行中的右括號。

maxMonthly = (balance * (1 + monthlyRate ** 12)/12 
+0

哦,我的天使如此尷尬。謝謝。 –