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,260 points
3,009 views

1 answer to this question.

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,820 points

Related Questions In Python

+1 vote
5 answers

how to exit a python script in an if statement

Instead of using the normal UTF-8 encoding, ...READ MORE

answered Jul 4, 2023 in Python by bodymist
• 140 points
355,409 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,560 points

edited Dec 6, 2018 by Nymeria 16,750 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
1,258 views
0 votes
1 answer

How to use for loop in Python?

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

answered Mar 4, 2019 in Python by Priyaj
• 58,020 points
746 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
• 13,300 points
867 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 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,820 points
724 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,820 points
783 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP