首页 文章

在Python中模拟一个do-while循环?

提问于
浏览
604

我需要在Python程序中模拟do-while循环 . 不幸的是,以下简单的代码不起作用:

list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None

while True:
  if element:
    print element

  try:
    element = iterator.next()
  except StopIteration:
    break

print "done"

而不是“1,2,3,完成”,它打印以下输出:

[stdout:]1
[stdout:]2
[stdout:]3
None['Traceback (most recent call last):
', '  File "test_python.py", line 8, in <module>
    s = i.next()
', 'StopIteration
']

为了捕获'stop iteration'异常并正确地打破while循环,我该怎么办?

以下将伪代码示为可能需要这样的事物的示例 .

状态机:

s = ""
while True :
  if state is STATE_CODE :
    if "//" in s :
      tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
      state = STATE_COMMENT
    else :
      tokens.add( TOKEN_CODE, s )
  if state is STATE_COMMENT :
    if "//" in s :
      tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
    else
      state = STATE_CODE
      # Re-evaluate same line
      continue
  try :
    s = i.next()
  except StopIteration :
    break

13 回答

  • 30

    我的下面的代码可能是一个有用的实现,突出显示do-whilewhile之间的主要区别,据我所知 .

    所以在这种情况下,你总是至少经历一次循环 .

    first_pass = True
    while first_pass or condition:
        first_pass = False
        do_stuff()
    
  • 6

    你为什么不这样做呢

    for s in l :
        print s
    print "done"
    

  • 16
    while condition is True: 
      stuff()
    else:
      stuff()
    
  • 1

    我这样做的方式如下......

    condition = True
    while condition:
         do_stuff()
         condition = (<something that evaluates to True or False>)
    

    在我看来这是一个简单的解决方案,我很惊讶我已经没有在这里看到它 . 这显然也可以反过来

    while not condition:
    

    等等

  • 7
    do {
      stuff()
    } while (condition())
    
    while True:
      stuff()
      if not condition():
        break
    

    你可以做一个功能:

    def do_while(stuff, condition):
      while condition(stuff()):
        pass
    

    但是1)它是's ugly. 2) Condition should be a function with one parameter, supposed to be filled by stuff (it'是不使用经典while循环的唯一原因 . )

  • 59

    我不确定你要做什么 . 您可以像这样实现do-while循环:

    while True:
      stuff()
      if fail_condition:
        break
    

    要么:

    stuff()
    while not fail_condition:
      stuff()
    

    你在尝试使用do while循环打印列表中的东西是做什么的?为什么不使用:

    for i in l:
      print i
    print "done"
    

    更新:

    你有一个行列表吗?你想继续迭代吗?怎么样:

    for s in l: 
      while True: 
        stuff() 
        # use a "break" instead of s = i.next()
    

    这看起来像你想要的东西吗?使用您的代码示例,它将是:

    for s in some_list:
      while True:
        if state is STATE_CODE:
          if "//" in s:
            tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
            state = STATE_COMMENT
          else :
            tokens.add( TOKEN_CODE, s )
        if state is STATE_COMMENT:
          if "//" in s:
            tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
            break # get next s
          else:
            state = STATE_CODE
            # re-evaluate same line
            # continues automatically
    
  • 251

    看看这是否有帮助:

    在异常处理程序中设置一个标志,并在处理s之前检查它 .

    flagBreak = false;
    while True :
    
        if flagBreak : break
    
        if s :
            print s
        try :
            s = i.next()
        except StopIteration :
            flagBreak = true
    
    print "done"
    
  • 10

    这是一个模拟do-while循环的非常简单的方法:

    condition = True
    while condition:
        # loop body here
        condition = test_loop_condition()
    # end of loop
    

    do-while循环的关键特性是循环体总是至少执行一次,并且在循环体的底部评估条件 . 这里显示的控制结构完成了这两个,不需要异常或break语句 . 它确实引入了一个额外的布尔变量 .

  • 1

    异常将打破循环,因此您可以在循环外处理它 .

    try:
      while True:
        if s:
          print s
        s = i.next()
    except StopIteration:   
      pass
    

    我猜您的代码问题是 break 内部 break 的行为未定义 . 通常 break 仅上升一级,例如 break 里面 try 直接进入 finally (如果存在)一个 try ,但不是循环 .

    相关PEP:http://www.python.org/dev/peps/pep-3136
    相关问题:Breaking out of nested loops

  • 763

    对于包含try语句的do-while循环

    loop = True
    while loop:
        generic_stuff()
        try:
            questionable_stuff()
    #       to break from successful completion
    #       loop = False  
        except:
            optional_stuff()
    #       to break from unsuccessful completion - 
    #       the case referenced in the OP's question
            loop = False
       finally:
            more_generic_stuff()
    

    或者,当不需要'finally'条款时

    while True:
        generic_stuff()
        try:
            questionable_stuff()
    #       to break from successful completion
    #       break  
        except:
            optional_stuff()
    #       to break from unsuccessful completion - 
    #       the case referenced in the OP's question
            break
    
  • 5

    这是一个不同模式的疯狂解决方案 - 使用协同程序 . 代码仍然非常相似,但有一个重要区别;根本没有退出条件!当你停止向数据提供数据时,协程(真正的协同链)就会停止 .

    def coroutine(func):
        """Coroutine decorator
    
        Coroutines must be started, advanced to their first "yield" point,
        and this decorator does this automatically.
        """
        def startcr(*ar, **kw):
            cr = func(*ar, **kw)
            cr.next()
            return cr
        return startcr
    
    @coroutine
    def collector(storage):
        """Act as "sink" and collect all sent in @storage"""
        while True:
            storage.append((yield))
    
    @coroutine      
    def state_machine(sink):
        """ .send() new parts to be tokenized by the state machine,
        tokens are passed on to @sink
        """ 
        s = ""
        state = STATE_CODE
        while True: 
            if state is STATE_CODE :
                if "//" in s :
                    sink.send((TOKEN_COMMENT, s.split( "//" )[1] ))
                    state = STATE_COMMENT
                else :
                    sink.send(( TOKEN_CODE, s ))
            if state is STATE_COMMENT :
                if "//" in s :
                    sink.send(( TOKEN_COMMENT, s.split( "//" )[1] ))
                else
                    state = STATE_CODE
                    # re-evaluate same line
                    continue
            s = (yield)
    
    tokens = []
    sm = state_machine(collector(tokens))
    for piece in i:
        sm.send(piece)
    

    上面的代码在 tokens 中收集所有标记作为元组,我假设原始代码中的 .append().add() 之间没有区别 .

  • 30

    如果您正处于循环而资源不可用或类似的情况下抛出异常的情况,您可以使用类似的东西

    import time
    
    while True:
        try:
           f = open('some/path', 'r')
        except IOError:
           print('File could not be read. Retrying in 5 seconds')   
           time.sleep(5)
        else:
           break
    
  • 4

    快速入侵:

    def dowhile(func = None, condition = None):
        if not func or not condition:
            return
        else:
            func()
            while condition():
                func()
    

    使用如下:

    >>> x = 10
    >>> def f():
    ...     global x
    ...     x = x - 1
    >>> def c():
            global x
            return x > 0
    >>> dowhile(f, c)
    >>> print x
    0
    

相关问题