Python Forum

Full Version: I hate "List index out of range"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
That is not at all what your program does. I would say your program uses the "least force" approach, only making one guess that is always guaranteed to be correct (as long as the input is limited to lower case ascii characters). At most, your program would have to make 130 (5 x 26) comparisons for a 5 letter password. A brute force approach for matching a password with 5 characters would test 5 letter permutations (with replacement) against the password. This might require millions of guesses (almost 12 million) to find a match. Allow upper and lowercase letters and some punctuation and there are over 650 million possible 5 letter passwords. That's almost nothing compared to the number 7 letter passwords you could make (over 2 trillion).

There is a Python library for generating permutations, or you could do that yourself.

As for your code, it is more of a filter, eliminating letters from the input password that are not lower case ascii. It could be written like this:
import string

word = input("Enter your password : ")
chain = ""
for letter in word:
    for character in string.ascii_lowercase:
        if character == letter:
            chain += letter
            break
print(chain)
Though it would be more efficient to use "in" in place of the "for character" loop.
import string

word = input("Enter your password : ")
chain = ""
for letter in word:
    if letter in string.ascii_lowercase:
        chain += letter
print(chain)
And this could really just be a couple of lines.
import string

word = input("Enter your password : ")
chain = "".join(c for c in word if c in string.ascii_lowercase)
print(chain)
Notice there is no indexing. I find indexing is rarely used in Python, at least compared to other languages.
Pages: 1 2 3