Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - 在Python中模拟do-while循环?(Emulate a do-while loop in Python?)

I need to emulate a do-while loop in a Python program.

(我需要在Python程序中模拟do-while循环。)

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:

(而不是“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
']

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

(我能做些什么来捕获'stop iteration'异常并正确地打破while循环?)

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
  ask by grigoryvp translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not sure what you are trying to do.

(我不确定你要做什么。)

You can implement a do-while loop like this:

(您可以像这样实现do-while循环:)

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?

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...