在if语句中中断for循环 [英] Break for loop in an if statement
问题描述
当前在中断此for
循环时遇到麻烦.如果要在此列表中找不到该变量,我想中断它,以便它可以移动另外两个for
循环.它期望在for
循环的顶部有一个缩进的块,但是如果我更改break
或for
循环的开始的位置,则它不起作用.救命!
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
asp
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
这篇关于在if语句中中断for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!