将 'continue' 从函数内部传递到 Python 中的外部循环
Pass 'continue' from inside a function to the outer loop in Python
我希望我能相应地表达自己,因为我对编程还很陌生,我确信答案很简单,但我做不到...:)
所以我有一个执行某些功能的 'for' 循环。每个函数中都有一个 'if' 语句,并且只在条件允许的情况下做一些事情。
我想要的是在满足函数的 'if' 语句后立即重置循环。
编辑:我把函数搞砸了 :( 谢谢你到目前为止的回答 ^^
我希望可行的示例:
def do_something():
if something == True:
do_some_stuff
return continue
while i < 999:
do_something()
do_something2()
do_something3()
'return continue' 是无法按我的意愿工作的部分,我找不到解决方案(也许我不知道 google 的具体用途)
没有功能,所以return
也没什么。但是,一旦满足条件 something
,您就可以 break
退出 while
循环:
while i < 999:
if something == True:
do_some_stuff
break
你可以有 'do_something' return 一个布尔值
def do_something():
if something is True:
do_some_stuff
return True
return False
while i < 999:
if do_something():
continue
do_something2()
do_something3()
我希望我能相应地表达自己,因为我对编程还很陌生,我确信答案很简单,但我做不到...:) 所以我有一个执行某些功能的 'for' 循环。每个函数中都有一个 'if' 语句,并且只在条件允许的情况下做一些事情。 我想要的是在满足函数的 'if' 语句后立即重置循环。
编辑:我把函数搞砸了 :( 谢谢你到目前为止的回答 ^^ 我希望可行的示例:
def do_something():
if something == True:
do_some_stuff
return continue
while i < 999:
do_something()
do_something2()
do_something3()
'return continue' 是无法按我的意愿工作的部分,我找不到解决方案(也许我不知道 google 的具体用途)
没有功能,所以return
也没什么。但是,一旦满足条件 something
,您就可以 break
退出 while
循环:
while i < 999:
if something == True:
do_some_stuff
break
你可以有 'do_something' return 一个布尔值
def do_something():
if something is True:
do_some_stuff
return True
return False
while i < 999:
if do_something():
continue
do_something2()
do_something3()