如何打破python中的无限循环?

时间:2015-08-05 04:21:43

标签: python python-2.7 infinite-loop openerp-7

以下代码是我的方法定义,它将计算/获取下一个日期取决于用户输入的条款。我现在的问题是,在运行它时返回一个无限循环,日期没有改变。这与我之前的问题有某种关系,但这次我点了一个按钮。

def calculate_schedule(self, cr, uid, ids, context=None):
    schedule_obj = self.pool.get('installment.schedule')
    sequence_obj = self.pool.get('ir.sequence')
    id = []
    _logger.info("\n\t\t\t1st .. IDS %s"%(str(ids)))
    for record in self.browse(cr, uid, ids, context=context):
        itb = record.name or sequence_obj.get(cr, uid, 'installment')
        _logger.info("\n\t\t\t2nd .. ITB %s"%(str(itb)))
        old_history = schedule_obj.search(cr, uid, [('parent_id','=',record.id)],context=context)
        _logger.info("\n\t\t\t3rd .. OLD HISTORY %s"%(str(old_history)))
        if old_history:
            schedule_obj.unlink(cr, uid, old_history, context=context)
        factor = self._get_factor(cr, uid, ids, record.terms, context=context)
        i =  0
        seq = 1
        range = 24

        while seq <= 24:
            while i < factor:
               date = datetime.strptime(record.payable_start_on, '%Y-%m-%d') + relativedelta(months=+i)
               _logger.info("\n\t\t\t4th .. Date %s"%(str(date)))
               key = str(record.id)
               temp_dict = {
                         'regular_date' : date.strftime('%Y-%m-%d'),
                         'seq' : seq,
                         'parent_id' : record.id,
                         }
               _logger.info("\n\t\t\t5th .. TEMP DICT %s"%(str(temp_dict)))
               id = schedule_obj.create(cr,uid,temp_dict,context=context)
               _logger.info("\n\t\t\t6th .. CREATED SCHEDULE %s"%(str(id)))  
            i += 1
        seq += 1
        #break
        lines = [(0,0,line) for line in schedule_obj.search(cr,uid,[('id','=',id[0])])]
        _logger.info("\n\t\t\t7th .. LINES %s"%(str(lines)))
        self.write(cr,uid,[record.id],{'child_ids':lines},context=context)
    return  True

1 个答案:

答案 0 :(得分:3)

你的评论者已经敲了敲头。

在python中,您的代码根据缩进决定运行的顺序,而不是括号或其他格式。因此,如果seq += 1i += 1与while循环具有相同的缩进,则不会在while循环中运行它们,而是在外部运行它们。因此,当while循环条件基于这些变量时,您永远不会退出循环。

解决方案只是将这两个语句缩进到while循环中。