gri*_*yvp 731 python while-loop do-while
我需要在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
Tom*_*Tom 905
我不确定你要做什么.您可以像这样实现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
小智 290
这是一个模拟do-while循环的非常简单的方法:
condition = True
while condition:
    # loop body here
    condition = test_loop_condition()
# end of loop
do-while循环的关键特性是循环体总是至少执行一次,并且在循环体的底部评估条件.这里显示的控制结构完成了这两个,不需要异常或break语句.它确实引入了一个额外的布尔变量.
eva*_*n54 72
我下面的代码可能是一个有用的实施,强调的主要区别DO-而 VS 而据我所知.
所以在这种情况下,你总是至少经历一次循环.
first_pass = True
while first_pass or condition:
    first_pass = False
    do_stuff()
var*_*tec 33
异常将打破循环,因此您可以在循环外处理它.
try:
  while True:
    if s:
      print s
    s = i.next()
except StopIteration:   
  pass
我猜你的代码的问题是没有定义break内部行为except.通常break只有一个级别,所以例如break内部try直接进入finally(如果存在)一个try,但不是循环.
相关PEP:http 
 
://www.python.org/dev/peps/pep-3136相关问题:突破嵌套循环
小智 30
do {
  stuff()
} while (condition())
- >
while True:
  stuff()
  if not condition():
    break
你可以做一个功能:
def do_while(stuff, condition):
  while condition(stuff()):
    pass
但是1)这很难看.2)条件应该是一个带有一个参数的函数,应该由stuff填充(这是不使用经典while循环的唯一原因.)
Dan*_*ano 18
我相信 python 上的 do-while 模拟的语法格式最接近 C 和 Java 中的 do-while 结构格式。
do = True
while do:
    [...]
    do = <condition>
u0b*_*6ae 16
这是一个不同模式的疯狂解决方案 - 使用协同程序.代码仍然非常相似,但有一个重要区别; 根本没有退出条件!当您停止向数据提供数据时,协程(协程链确实)就会停止.
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().
小智 12
我这样做的方式如下......
condition = True
while condition:
     do_stuff()
     condition = (<something that evaluates to True or False>)
在我看来这是一个简单的解决方案,我很惊讶我已经没有在这里看到它.这显然也可以反过来
while not condition:
等等
Mar*_*ark 10
对于包含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
小智 9
Python 3.8 给出了答案。
它被称为赋值表达式。从文档:
# Loop over fixed length blocks
while (block := f.read(256)) != '':
    process(block)
while 循环:
while condition:
  print("hello")
  
执行 while 循环:
while True:
  print("hello")
  if not condition:
    break
您也可以使用任何 true 布尔值作为条件:
while 1:
  print("hello")
  if not condition:
    break
另一种变体:
check = 1
while check:
    print("hello")
    check = condition
while condition is True: 
  stuff()
else:
  stuff()
快速破解:
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