全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
9946 4
2016-11-23
# 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 没有指明是对哪个变量,所以是全局变量还是什么?

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-11-23 16:33:04
True是布尔值, while True : 就是一直执行循环, 死循环
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-11-24 17:44:37
具体内容不谈,应该是一个整体的测试阶段,这样设定为True就可以跑这一段逻辑来看结果,如果后续这一段不需要跑了,将True改为False即可。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-7-20 15:11:38
while true 就是你不中断 就一直循环
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-7-25 19:24:33
while True意思是要一直进行死循环,一般while true 循环中会加入break条件判断用以在循环内部的某个条件达成时终止循环。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群