# Use bisection search until the search space is sufficiently small
while True:
balance = original_balance
monthly_payment = (low_payment + high_payment)/2
# Simulate passage of time until outstanding balance is paid off
# Each iteration represents 1 month
for month in range(1,13):
interest = round(balance*interest_rate/12, 2)
balance += interest - monthly_payment
if balance <= 0:
break
if (high_payment - low_payment < 0.005):
# Bisection search space is small enough
# Print result
print "RESULT"
# Round monthly payment up to the nearest cent
monthly_payment = round(monthly_payment + 0.004999, 2)
print "Monthly payment to pay off debt in 1 year:", round(monthly_payment,2)
# Recompute remaining balance and the number of months needed
balance = original_balance
for month in range(1,13):
interest = round(balance*interest_rate/12, 2)
balance += interest - monthly_payment
if balance <= 0:
break
print "Number of months needed:", month
print "Balance:", round(balance,2)
break
elif balance < 0:
#Paying too much
high_payment = monthly_payment
else:
#Paying too little
low_payment = monthly_payment
这长串代码是用二分法找break-even的点。具体内容不用管。刚开始学python,所以有点搞不明白while true这个条件语句到底是什么意思。如果True 的意思是不等于0,也是说得通的,就是balance不等于0的话就一直run。但他只写while true 没有指明是对哪个变量,所以是全局变量还是什么?