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
809 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,810 points

Related Questions In Python

0 votes
3 answers

how to exit a python script in an if statement

In Python, the break statement provides you with the opportunity ...READ MORE

answered Dec 13, 2020 in Python by Roshni
• 10,420 points
61,711 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,520 points

edited Dec 6, 2018 by Nymeria 12,493 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
259 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,100 points
123 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,270 points
113 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,985 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,810 points
165 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,810 points
186 views