我正在尝试编写一个函数(在Python 2.7中),该函数具有未偿还的余额和年利率,然后使用二等分搜索 to solve problem #3将每月的最小付款额返回至最近的分。我正在尝试通过在主函数内编写一个函数来遵循DRY原则,该函数应在一年后返回一个包含余额和月数的列表(如果余额为零或更少,循环应该中断),这需要计算两次在我的主要职能上。当我尝试在继续操作之前测试此初始封闭时,在分配monthlyPayment
的行上收到语法错误。我究竟做错了什么?
# 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"))
最佳答案
您忘记了前一行的右括号。
maxMonthly = (balance * (1 + monthlyRate ** 12 )/12
关于python-2.7 - 尝试编写Python闭包的语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40810829/