-1

I have made small script with Python that brute forces given word and when it finds the word, it should stop.

from itertools import product
rangeone = int(raw_input("Set range1: "))
rangetwo = int(raw_input("Set range2: "))

question = raw_input("Give password: ")

alphabets = 'abcdefghijklmnopqrstuvwxyz1234567890'

for random in range(rangeone, rangetwo):
    loops = product(alphabets, repeat=random)
    for y in loops:
        password = ''.join(y)
        if password == question:
            print "done"
            break

But, it doesn't break the loop. It keeps going

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
fedora man
  • 33
  • 1
  • 6
  • Possible duplicate of [How to break out of multiple loops in Python?](http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python) – d33tah Apr 25 '16 at 19:51

1 Answers1

0

Your code has two loops, and the break can only breaks the inner loop. You could define a variable (e.g. stop). Once the word is found, you set it to True, to stop the outer loop:

from itertools import product
rangeone = int(raw_input("Set range1: "))
rangetwo = int(raw_input("Set range2: "))

question = raw_input("Give password: ")

alphabets = 'abcdefghijklmnopqrstuvwxyz1234567890'

stop = False
for random in range(rangeone, rangetwo):
    loops = product(alphabets, repeat=random)
    for y in loops:
        password = ''.join(y)
        if password == question:
            print "done"
            stop = True
            break   # breaks the inner loop
    if stop: break  # breaks the outer loop
Quinn
  • 4,394
  • 2
  • 21
  • 19