Question

So I have made a code that is sort of a brute force password guesser that is made to guess a 3 letter password.

I know this code is not correct because the while statement is obviously wrong so how could I fix that?

Also how could I make this code interact with other programs?

For example:

Winrar has some files that need a 3 word password and has an enter screen where you type the password. How could I make this my code interact or 'type' within that password enter box?

import random
import string

while password != correct: 
    letter 1 = random.choice(string.letters)
    letter 2 = random.choice(string.letters)
    letter 3 = random.choice(string.letters)
    word = '%s%s%s'%(letter1, letter2, letter3) 
    if word == password:
        print "The code is %s"%(word) 
    else:
        break; 
Was it helpful?

Solution

This is a terrible way to go about this, but....

import random
import string

while True:
    # Do it until the `if` block says it's right, then break.
    word = ''.join([random.choice(string.letters) for _ in range(3)])
    if word == password:
        print "The code is %s" % (word)
        break

EDIT: ''.join([random.choice(string.letters) for _ in range(3)]) explained, as per your request in the comments.

[x for x in list_of_xs] is a list comprehension. You can do transformations on the resultant item (for example, all perfect squares from roots 1-10 is [x**2 for x in range(1,11)] or more simply [x**2 for x in [1,2,3,4,5,6,7,8,9,10] ]. There's lots of info out there on this, and tons of canonical SO answers.

random.choice(string.letters) you already use, so that much you get. ''.join is using the str.join command, which combines an iterable of strings into one string, using the str as a separator. ", ".join(["Eggs","Cheese","Bread","Milk"]) results in "Eggs, Cheese, Bread, Milk". In this case I'm using an empty string to combine.

Basically I'm building a list of three random.choice(string.letters) (the _ isn't anything special, it's just notation for the next programmer that comes along that this variable isn't necessary. [do_something for _ in range(x)] is a common idiom for a list of do_somethings x long), then combining them together with an empty string in between them (in other words, NOTHING in between them)

Maybe

import random
import string

while word != password:
    # do the same stuff

But honestly what you're trying to do is:

import itertools as it
for attempt in (''.join(combo) for combo in it.combinations(string.letters,3)):
    if attempt == password:
        print "success:",attempt
else:
    print "failure"

To pre-empt your question, itertools (which I imported as it) has a method combinations that returns all of the unique combinations of the iterable you hand it of the sequence length r. The notation is itertools.combinations(iterable,r). It returns a list of tuples, which then need to be combined in the same way I did above.

The only failing of THIS method is that it can't catch passwords like "aaa". Any repeated characters won't show up in combinations.

As far as your title: "How can I make this code interact with other programs" is too broad for this question. It's not a good fit unfortunately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top