如何使“continue”语句在找到语句的循环外迭代“mother”循环?(Python)

2024-04-25 03:41:23 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个这样的代码:

for-loop1:
    for-loop2:
        if-statement:
            continue
        <code here>
        <code there>

问题是,如何使continue语句迭代for-loop1而不仅仅是for-loop2?将语句与for-loop2行放在同一行将迭代for-loop1,甚至不首先执行其余的代码。帮助:(


Tags: 代码forifherecode语句statementthere
1条回答
网友
1楼 · 发布于 2024-04-25 03:41:23

如果for-loop2之后没有代码,break将有效地重新启动外部循环。你知道吗

for-loop1:
    for-loop2:
        if-statement:
            break
        <code here>
        <code there>

如果在内部循环之后有额外的东西,那么它将需要更多的工作。你知道吗

for-loop1:
    restart_loop1 = False

    for-loop2:
        if-statement:
            restart_loop1 = True
            break

        <code here>
        <code there>

    if restart_loop1:
        continue

    <more code here>

相关问题 更多 >