在 if 语句中中断 for 循环 [英] Break for loop in an if statement

查看:41
本文介绍了在 if 语句中中断 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前无法打破这个 for 循环.如果在此列表中找不到该变量,我想中断它,以便它可以移动另外两个 for 循环.它需要 for 循环顶部的缩进块,但是如果我更改 breakfor 开头的位置循环,它不起作用.帮助!

Currently having trouble with breaking this for loop. I want to break it if the variable is not found in this list so it can move two another for loop. It expects an indented block for the top of the for loop, but if I change the position of the break or of the start of the for loop, it doesn't work. Help!

while cyclenumb <= 10000:

    for x in userpassword[k]:
        for z in lowercaselist:
            if x in z:
                newpasswordlist.append(z)
                k +=1
                break
        else:

    for x in userpassword[k]:
        for z in uppercaselist:
            if x in z:
                newpasswordlist.append(z)
                k +=1
                break
        else:

推荐答案

你需要单独跳出每个循环,正如人们在你的问题的评论中提到的那样,break 只会停止它所在的循环

You'll need to break out of each loop separately, as people have mentioned in the comments for your question, break only stops the loop which it's in

for x in userpassword[k]:
    for z in lowercaselist:
        if x in z:
            newpasswordlist.append(z)
            k +=1
            break
   
     if x in z: # added an extra condition to exit the main loop
        break

您需要为两个循环

如果您也想跳出 while 循环,那么您也可以在该循环中添加 if x in z: break.

If you want to break out of the while loop as well, then you can add if x in z: break in that loop as well.

这篇关于在 if 语句中中断 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆