简体   繁体   English

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

[英]How to break an infinite loop in python?

Code below is my method definition that will calculate/get the next date depends on the terms entered by the user. 下面的代码是我的方法定义,它将根据用户输入的条款来计算/获取下一个日期。 My problem now is that upon running it returns an infinite loop and the date did not change. 我现在的问题是,在运行时它返回一个无限循环,并且日期没有更改。 This is somehow related with my previous question, but this time I put on a button. 这在某种程度上与我之前的问题有关,但是这次我按下了一个按钮。

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

Your commenters have already hit the nail on the head. 您的评论者已经触及到头了。

In python, your code decides the order in which to run based on indentation, not on brackets or other formatting. 在python中,您的代码根据缩进而不是括号或其他格式决定运行顺序。 Because of this, when you have seq += 1 and i += 1 with the same indentation as the while loop, you are not running them inside the while loops, you are running them outside. 因此,当您的seq += 1i += 1的缩进与while循环相同时,您不会在while循环内运行它们,而是在外部运行它们。 Thus as the while loop conditions are based on these variables, you will never exit your loops. 因此,由于while循环条件是基于这些变量的,因此您将永远不会退出循环。

The solution is simply to indent these two statements to be inside the while loops. 解决方案就是将这两个语句缩进while循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM