24

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:
mansa
  • 271
  • 1
  • 2
  • 6
  • 1
    `break` only breaks out of the inner loop, not the outer loop. Are you sure you need the inner loops? Maybe it should just be `if x in lowercaselist` – Barmar Dec 09 '16 at 17:02
  • 1
    At first glance, your indentation seems incorrect. Does the first else go against the first if x in z? Then it should be indented to the same level as the first if. Does the third for statement go under that else? Then everything should be indented one level deeper than the else. – rajah9 Dec 09 '16 at 17:03
  • What's the value of `lowercaselist` and `uppercaselist`? Are they lists of lists? That's whay it looks like when you're looping over them and then using `x in z`. – Barmar Dec 09 '16 at 17:07
  • barmar, they are lists containing the lower and upper cases of the alphabet, ex "a", "b", – mansa Dec 09 '16 at 17:08
  • Then why are you using `x in z` instead of just `x = z`? Or why don't you just write `if x in lowercaselist:`? – Barmar Dec 09 '16 at 17:09
  • that'll work, how am i supposed to append that value?? to a new list – mansa Dec 09 '16 at 17:11

1 Answers1

31

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 as well.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
  • Yeah, that means the index is too big for the list, for example if you have a `list` of 4 elements, and you try to get `list[5]` it will give an error because there is no 5th element – Tom Fuller Dec 09 '16 at 17:17
  • thank you, i was able to troubleshoot the index problem, but now, after i added the print newpasswordlist function after the last k+=1 n the if statement, it prints the value of z twice – mansa Dec 09 '16 at 17:20
  • thank you sir, i will add this too my multiple other for loops in my program, hopefully it will work – mansa Dec 09 '16 at 17:23