Y
Y
Yrets1692021-11-10 15:29:45
Python
Yrets169, 2021-11-10 15:29:45

How to exit the loop on match?

Good afternoon, I'm trying to create a simple enumeration of characters with a step, for example, from 1 to 4.
If the password is 4 characters, then everything works as it should, a match is found in the loop and its action is interrupted.
But if the password is less than 4 characters, then if it matches, the loop does not stop and continues to work.

sCharacters = "abcdefghijklmnopqrstuvwxyz0123456789"
for iCombinationLength in range(1, 4+1):
    for aCombination in itertools.product(sCharacters, repeat=iCombinationLength):
        guess_password = ''.join(aCombination)
        if (guess_password == "123"):
            print("Your password is : " + "".join(guess_password))
            break


How to stop the loop when the password matches if it is less than 4 characters?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yrets169, 2021-11-10
@Yrets169

On the recommendation of AWEme , moved the inner loop into a function with its subsequent completion if the password matches
(fully working version)

import itertools
sCharacters = "abcdefghijklmnopqrstuvwxyz0123456789"
def brute_pass():
    for aCombination in itertools.product(sCharacters, repeat=iCombinationLength):
        guess_password = ''.join(aCombination)
        print(''.join(aCombination))
        if (guess_password == "124"):
             print("Your password is : " + "".join(guess_password))
             return False

for iCombinationLength in range(1, 4+1):
    if brute_pass() ==False:
        break

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question