简体   繁体   English

执行一次后如何停止if循环?

[英]How would I stop a if loop after execute one time?

if len(columns) > 0:
    #print "IF LOOP"
    for index in range(len(columns)):
          txt = columns[index]
          msg = msg + txt + '  Declared'
    msg = ' '.join(msg.split())
    notifier(msg)

I created one cronjob in my program but i want to stop execute this loop after one time. 我在程序中创建了一个cronjob,但是我想在一段时间后停止执行此循环。

Suppose i create new variable in starting and after one time execute this loop assign False so if loop checks two conditions if Variable is False so it will not execute this statement.... 假设我在启动时创建了一个新变量,并在执行一次此循环后将其赋值为False因此如果循环检查两个条件(如果Variable为False ,则它将不会执行此语句。

but every time cronjob creates new objects so what is the solution for this to stop execute this statement in if loop after one time executed. 但是每次cronjob创建新对象时,要解决此问题的解决方案是在执行一次后在if循环中停止执行此语句。

Use break if you want to exit a loop (either while or for ). 如果要退出循环( whilefor ),请使用break A very simple example: 一个非常简单的例子:

count = 0

for _ in range(1000):
    count += 1
    if count == 1:
        break

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

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