How to break for loop in an if statement

0 votes

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 forloop, 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:

Nov 16, 2018 in Python by findingbugs
• 3,140 points
18 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can break out of each loop separately.

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 

answered Nov 16, 2018 by Jino
• 5,520 points

Related Questions In Python

0 votes
1 answer

how to exit a python script in an if statement

This works fine for me: while True: ...READ MORE

answered Sep 19, 2018 in Python by bug_seeker
• 14,970 points
225 views
0 votes
1 answer

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

answered Dec 4, 2018 in Python by Nymeria
• 3,500 points

edited Dec 6, 2018 by Nymeria 473 views
0 votes
1 answer

How to use nested if else statement in python?

The working of nested if-else is similar ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
27 views
0 votes
1 answer

How to use for loop in Python?

There are multiple ways of using for ...READ MORE

answered Mar 4 in Python by Priyaj
• 56,120 points
9 views
0 votes
1 answer

Break for loop in an if statement

You'll need to break out of each ...READ MORE

answered Oct 23, 2018 in Python by SDeb
• 9,540 points
13 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
20 views
0 votes
1 answer

Connect to a MySQL using Python?

Very similar to mysqldb but better than ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,520 points
26 views
0 votes
1 answer

Open file in Python

There is this code I used for ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,520 points
17 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.