如何模拟do-while循环? [英] How to emulate a do-while loop?

查看:65
本文介绍了如何模拟do-while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  list_of_ints = [1,2,3]迭代器= list_of_ints .__ iter __()元素=无而True:如果元素:打印元素尝试:元素= iterator.next()StopIteration除外:休息打印完成" 

代替"1,2,3,done",它输出以下输出:

  [stdout:] 1[标准输出:] 2[标准输出:] 3无['追踪(最近通话):','文件"test_python.py",第8行,在< module>s = i.next()','StopIteration'] 

我该怎么办才能捕获停止迭代"异常并暂停一会儿循环正常吗?

为什么需要这样的东西的示例在下面显示为伪代码.

状态机:

  s ="而True:如果状态为STATE_CODE:如果s中为"//":tokens.add(TOKEN_COMMENT,s.split("//")[1])状态= STATE_COMMENT别的 :tokens.add(TOKEN_CODE,s)如果状态为STATE_COMMENT:如果s中为"//":tokens.append(TOKEN_COMMENT,s.split("//")[1])别的状态= STATE_CODE#重新评估同一行继续尝试 :s = i.next()StopIteration除外:休息 

解决方案

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

 而True:东西()如果失败条件:休息 

或者:

  stuff()而不是fail_condition:东西() 

您正在尝试使用do while循环来打印列表中的内容吗?为什么不只是使用:

l中的i的

 :打印我打印完成" 

更新:

那么您有行列表吗?而您想继续遍历它呢?怎么样:

l中s的

 :而True:东西()#使用"break"代替s = i.next() 

这看起来像您想要的东西吗?在您的代码示例中,将是:

for some_list中的:而True:如果状态为STATE_CODE:如果s中为"//":tokens.add(TOKEN_COMMENT,s.split("//")[1])状态= STATE_COMMENT别的 :tokens.add(TOKEN_CODE,s)如果状态为STATE_COMMENT:如果s中为"//":tokens.append(TOKEN_COMMENT,s.split("//")[1])打破#得到下一个别的:状态= STATE_CODE#重新评估同一行#自动继续

I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:

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"

Instead of "1,2,3,done", it prints the following output:

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

What can I do in order to catch the 'stop iteration' exception and break a while loop properly?

An example of why such a thing may be needed is shown below as pseudocode.

State machine:

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

解决方案

I am not sure what you are trying to do. You can implement a do-while loop like this:

while True:
  stuff()
  if fail_condition:
    break

Or:

stuff()
while not fail_condition:
  stuff()

What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:

for i in l:
  print i
print "done"

Update:

So do you have a list of lines? And you want to keep iterating through it? How about:

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

Does that seem like something close to what you would want? With your code example, it would be:

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

这篇关于如何模拟do-while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆