python-2.7 - 尝试编写Python闭包的语法无效

标签 python-2.7 closures syntax-error

我正在尝试编写一个函数(在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/

相关文章:

python - Python读取文件,但命令行打印空白行

mysql - 找不到mySQL请求错误

python - 字典数据未正确附加到另一个字典

python - 计算 S3 存储桶中的键

angularjs:$timeout 用法(服务内部)

unit-testing - 在 Groovy 中,什么时候使用 Expando 与 'as' 运算符和闭包比较有意义?

Python:使用 lineno 引发 SyntaxError

oracle - PLSQL : syntax problems with trigger

python - 如何有效地更改未知深度列表中最右边的值?

jquery - 如何将 jquery 自动完成与 var 一起使用?