我正在尝试编写一个函数(在Python 2.7中),该函数使用未结余额和年利率,然后使用二分搜索 to solve problem #3将最小月付款返回到最接近的分数。我试图通过在main函数中编写一个函数来遵循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"))
答案 0 :(得分:1)
您忘记了上一行的结束括号。
var result = cmd.ExecuteScalar();