Python Forum
How to break out of nested loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to break out of nested loops (/thread-32751.html)

Pages: 1 2


How to break out of nested loops - pace - Mar-03-2021

I'm struggling with a piece of code that I am writing for a computer security course.
I am trying to write a piece of test code that test a 4 digit password based on the time to resolve.

I want to start with a pw of 4 numbers all 0's. For each digit, test the number 0 through to 9.
If the I get a response back from a function that is greater than 0.2sec, then alter the pw list with that digit, then move onto the next one.

However my code is not incrementing through the list. I tried simply inserting a break after my if statement, but realized I also have to break our of the outer "for loop", i tried inserting a break here, but still cant get my code to work.

What Am I missing?

import time
import sys # ignore

sys.path.insert(0,'.') # ignore
real_password = '2234'

#This function cannot be changed
def check_password(password): # Don't change it
    if len(password) != len(real_password):
        return False
    for x, y in zip(password, real_password):
        time.sleep(0.1) # Simulates the wait time of the safe's mechanism
        if int(x) != int(y):
            return False
    return True



def crack_password():
    pw = [0,0,0,0]
    cnt = 0
    if cnt < 3:
        for i in range (0,9):
            pw[cnt] = i
            start = time.time()
            check_password(create_string(pw))
            stop = time.time()
            t_time = stop - start
            print(pw[cnt], t_time)
            if t_time > 0.2:
                cnt +=1
                {break out of the loop and move to the next digit}
                #Struggling with my code here!


#Creates a string from the list
def create_string(pw_list):
    string = ''
    for char in pw_list:
        string += str(char)
    return string
My print statement in the crack_password function provides:

0 0.10016393661499023
1 0.10017132759094238
2 0.1006174087524414
3 0.10022568702697754
4 0.1002652645111084
5 0.20053935050964355
6 0.10025167465209961
7 0.10107183456420898
8 0.10021018981933594

Thus I know that the first number of the password is 5, as it is greater than 0.2. How can I then break from this loop and move to the next iteration of the pw integer? I'm also interested in how other would solve this issue.


RE: How to break out of nested loops - deanhystad - Mar-03-2021

You need an outer loop to repeat the cracking code foor each digit of the 4 digit code. Break will do exactly what you want for the "inner" loop, ending the search for a digit and moving on to the next.


RE: How to break out of nested loops - pace - Mar-03-2021

(Mar-03-2021, 12:29 PM)deanhystad Wrote: You need an outer loop to repeat the cracking code foor each digit of the 4 digit code. Break will do exactly what you want for the "inner" loop, ending the search for a digit and moving on to the next.

Thanks for the response, I just added an extra line of code, an if statement under the for loop. This is the line of code I am having trouble breaking out of, as i need to also break out of the for loop after this.


RE: How to break out of nested loops - deanhystad - Mar-03-2021

Why aren't you using a for loop for the outer loop?

Don't change code in your posts. Make a new post for the modified code. Otherwise conversations that reference code get really confusing when the referenced code no longer exists.


RE: How to break out of nested loops - pace - Mar-03-2021

Yes agreed, an outside for loop would be better, how do I break these loops once the if statement is matched?

def crack_password():
    pw = [0,0,0,0]
    for x in range(4):
        for i in range (0,9):
            pw[x] = i
            start = time.time()
            check_password(create_string(pw))
            stop = time.time()
            t_time = stop - start
            #print(pw[x], t_time)
            if t_time > 0.2:
                break
   
            
    print(pw)



RE: How to break out of nested loops - deanhystad - Mar-03-2021

break? Just like you are doing?


RE: How to break out of nested loops - pace - Mar-03-2021

(Mar-03-2021, 04:42 PM)deanhystad Wrote: break? Just like you are doing?

That doesn't work, as it only breaks the inner loop and doesn't reset the loop for the next iteration.


RE: How to break out of nested loops - deanhystad - Mar-03-2021

You only want to break out of the inner loop. You want the outer loop to run until it finds all 4 numbers. And it does reset the inner loop for the next iteration.

This comment is in your code "break out of the loop and move to the next digit"

And this code does exactly that.
            if t_time > 0.2:
                break



RE: How to break out of nested loops - pace - Mar-03-2021

(Mar-03-2021, 05:12 PM)deanhystad Wrote: You only want to break out of the inner loop. You want the outer loop to run until it finds all 4 numbers. And it does reset the inner loop for the next iteration.

This comment is in your code "break out of the loop and move to the next digit"

And this code does exactly that.
            if t_time > 0.2:
                break

The problem is that it also needs to break the next for loop that is still iterating over the numbers 0-9
If I don't break this then, for example I find the number 5, break out, and the outer loop continues form 6 onwards.


RE: How to break out of nested loops - deanhystad - Mar-03-2021

I don't know where you get that idea. When you break out of the inner loop it finishes the code in the outer loop (in your example there is none) and begins the next iteration in the outer loop. When the outer loop encounters the inner for loop it starts the inner for loop from the beginning (0). It cannot continue the inner loop where the inner loop left off because there is no mechanism to remember where the for loop left off when it was broken.

A simple example:
import random

for a in '1234':
    for b in 'abcd':
        print (a, b)
        if b == random.choice('abcd'):
            break
Output:
1 a 1 b 1 c 2 a 2 b 2 c 2 d 3 a 4 a 4 b 4 c 4 d
Notice that each time the outer loop changes (1, 2, 3, 4) that the inner loop restarts at "a". This is what your code will do. Each time you find a new digit for "pw", the inner loop will start over at 0.