模仿Python中的do-while循环?
-
09-09-2019 - |
题
我需要仿真在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
']
我能为了赶在“停止迭代”异常,并打破了,而做 循环正常?
的原因可能需要这样的事情的例子示如以下伪代码。
状态机:
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
解决方案
我不知道你正在尝试做的。你可以实现一个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
其他提示
下面是模拟一个do-while循环非常简单的方法:
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
一do-while循环的主要特点是,所述循环体总是执行至少一次,并且该条件是在环体的底面进行评价。这里的控制结构显示,无需例外实现这两种或break语句。它引入一个额外的布尔变量。
我的代码可能是一个有用的实施,强调的主要区别<一类=“标签后的” href =“/问题/标记/ DO-而”标题=“显示问题标记‘做,而’”相对= “标签”>做,而 VS 而据我所知。
因此,在这种一种情况下,你总是通过循环至少一次。
first_pass = True
while first_pass or condition:
first_pass = False
do_stuff()
异常会打破循环,所以你还不如处理一下外循环。
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 结果 相关的问题:嵌套循环突围
do {
stuff()
} while (condition())
- >
while True:
stuff()
if not condition():
break
可以做的函数:
def do_while(stuff, condition):
while condition(stuff()):
pass
但是 1)它是丑陋的。 2)条件应与一个参数的函数,假定由东西被填充(它是唯一的原因不的使用而循环。经典的)
下面是一种不同的图案的更疯狂溶液 - 使用协同程序。该代码仍然是非常相似的,但有一个重要的区别;有根本没有退出条件!协程(协程真链)时,停止与数据供给它只是停止。
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()
之间没有差异。
我这样做的方法是如下...
condition = True
while condition:
do_stuff()
condition = (<something that evaluates to True or False>)
这在我看来是简单的解决方案,我很惊讶,我还没有看到它在这里了。这可以明显地也被反转,以
while not condition:
等
有一个do - 而含有try语句环
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()
可选地,当没有必要为“最终”条款
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
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
为什么你不只是做
for s in l :
print s
print "done"
请参阅如果这有助于:
设置一个标志的异常处理程序的内部和上一次一工作之前检查它。
flagBreak = false;
while True :
if flagBreak : break
if s :
print s
try :
s = i.next()
except StopIteration :
flagBreak = true
print "done"
如果你在你的循环方案而资源是unavaliable或类似的东西,抛出一个异常,你可以使用类似
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