Python Forum

Full Version: Brute Force Pad Lock Guesser
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to learn how to have more control over increments, but I have a delema (see comments in following code):

#!/usr/bin/env python3
#PadLockGuesser.py

lockCombo = 1080

#brute force pad lock guesser with while loop:
guess = 0
while guess != lockCombo:
    #guess += 1#placing the guess incrementor here causes 0 to not be one of the guesses
    if guess == lockCombo:
        print("After " + str(guess) + " guesses, the correct combo is " + str(guess))
        break
    else:
        print(str(guess) + " is not the correct combo. Trying next guess.")
        guess += 1#placing the guess incrementor here causes the program to stop guessing at 1079
        continue

#brute force pad lock guesser with for loop:
guessAgain = 0
for i in range(0000,9999):
    if guessAgain == lockCombo:
        print("After " + str(guessAgain) + " guesses, the correct combo is " + str(guessAgain))
        break
    else:
        print(str(guessAgain) + " is not the correct combo. Trying next guess.")
        guessAgain += 1
The output of the for loop, however, is correct:
Output:
0 is not the correct combo. Trying next guess. 1 is not the correct combo. Trying next guess. 2 is not the correct combo. Trying next guess. 3 is not the correct combo. Trying next guess. ... 1077 is not the correct combo. Trying next guess. 1078 is not the correct combo. Trying next guess. 1079 is not the correct combo. Trying next guess. After 1080 guesses, the correct combo is 1080
How do I get the while loop to produce the same output?
your while loop wont print out After 1080 guesses... with that !=, try other comparison operator instead?
(Feb-26-2018, 06:48 AM)ka06059 Wrote: [ -> ]your while loop wont print out After 1080 guesses... with that !=, try other comparison operator instead?
I forgot that I wasn't comparing strings, which means the <= operator worked!

Does anybody know any formatting techniques that would cause the output to be four digits the whole time?
For instance, how could I turn this output:
Output:
0 is not the correct combo. Trying next guess. 1 is not the correct combo. Trying next guess. 2 is not the correct combo. Trying next guess. 3 is not the correct combo. Trying next guess. ...
Into this output:
Output:
0000 is not the correct combo. Trying next guess. 0001 is not the correct combo. Trying next guess. 0002 is not the correct combo. Trying next guess. 0003 is not the correct combo. Trying next guess. ...
zfill is the answer:

#!/usr/bin/env python3
#PadLockGuesser.py

lockCombo = 1080

#brute force pad lock guesser with while loop:
guess = 0
while guess <= lockCombo:
    #guess += 1#placing the guess incrementor here causes 0 to not be one of the guesses
    if guess == lockCombo:
        print("After " + str(guess) + " guesses, the correct combo is " + str(guess))
        break
    else:
        print(str(guess).zfill(4) + " is not the correct combo. Trying next guess.")
        guess += 1#placing the guess incrementor here causes the program to stop guessing at 1079
        continue