Python Forum
I hate "List index out of range"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I hate "List index out of range"
#21
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,412 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,924 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,361 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,449 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,591 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,229 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,862 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,325 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,233 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,578 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020