循环返回? [英] return in loop for ?
问题描述
你好,
在for循环中返回是合法的
还是我必须在返回之前突破循环?
谢谢
yomgui
hello,
is it legal to return inside a for loop
or am I obliged to break out of the loop before returning ?
thanks
yomgui
推荐答案
yomgui< no * @ valid.org>写道:
yomgui <no*@valid.org> writes:
返回for循环是合法的
还是我必须在返回之前突破循环?
is it legal to return inside a for loop
or am I obliged to break out of the loop before returning ?
试一试,看看:
Try it and see:
def f():
.... for i在范围(20):
....如果我> 10:返回i
.... print f()
11
def f(): .... for i in range(20):
.... if i > 10: return i
.... print f() 11
< ;迈克
-
Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/
独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer写道:
Mike Meyer wrote:
< brom> yomgui< no*@valid.org>写道:
yomgui <no*@valid.org> writes:
在for循环中返回是合法的
还是我必须在返回之前突破循环?
is it legal to return inside a for loop
or am I obliged to break out of the loop before returning ?
尝试一下并看到:
Try it and see:
这不是因为它可以在python的实现上工作
它可以在任何其他平台上工作未来。
yomgui
it is not because it does work on an implementation of python
that it will work on any other platform or in the futur.
yomgui
yomgui< no*@valid.org>写道:
yomgui <no*@valid.org> writes:
Mike Meyer写道:
Mike Meyer wrote:
yomgui< no*@valid.org>写道:
yomgui <no*@valid.org> writes:
>返回for循环是否合法
>还是我不得不在返回之前突破循环?
> is it legal to return inside a for loop
> or am I obliged to break out of the loop before returning ?
尝试并看看:
Try it and see:
这不是因为它确实可以在python的实现上工作
这将适用于任何其他平台或未来。
it is not because it does work on an implementation of python
that it will work on any other platform or in the futur.
无论你如何得到答案,这都是真的。
>
< mike
-
Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/
独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。
That''s true no matter how you arrive at your answer.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
这篇关于循环返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!