Break for loop in an if statement

吃可爱长大的小学妹 提交于 2019-12-28 20:48:32

问题


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:

回答1:


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

You'll need to do this for both loops

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



来源:https://stackoverflow.com/questions/41065354/break-for-loop-in-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!