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"
#11
Also, you could use for loops to never run out of range:

fruit_list = ["banana", "apple", "coconut", "pineapple"]

for fruit in fruit_list:
    print(fruit)

# Also use enumerate() to work with indexes
for index, fruit in enumerate(fruit_list):
    print(f"{index} - {fruit}")
Output:
banana apple coconut pineapple 0 - banana 1 - apple 2 - coconut 3 - pineapple
Reply
#12
Hello everyone,

Thank you for your many answers, I looked at what you suggested but that does not seem to be the solution. Some have also given me advice and it will be useful, especially in the rest of my learning, so thank you!
I originally wanted to create my own brute force program to train

Regarding the code, it is true that it is not very clever of me not to put it so here it is below.

I applied Bowlofred's advice to see where the problem is. So I see that the loop continues (and the i += 1 as well). So it's weird because normally the "while" should stop it. Is it the "for" loop blocking this?

list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

word = input("Enter your password : ")
word = [caractere for caractere in word]
chain = ""
i = 0
while i < len(word):
	for l in list:
		print("i is egal to ", i, " and the length is", len(word))
		print("i is egal to ", l, " and the length is", word[i], "\n \n")
		print("the complete chain is", chain)
		if l == word[i]:
			chain += l
			if word != chain:
				i += 1
print(chain)
[/python]

Output :

Enter your password : def
i is egal to 0 and the length is 3
i is egal to a and the length is d


the complete chain is
i is egal to 0 and the length is 3
i is egal to b and the length is d


the complete chain is
i is egal to 0 and the length is 3
i is egal to c and the length is d


the complete chain is
i is egal to 0 and the length is 3
i is egal to d and the length is d


the complete chain is
i is egal to 1 and the length is 3
i is egal to e and the length is e


the complete chain is d
i is egal to 2 and the length is 3
i is egal to f and the length is f


the complete chain is de
i is egal to 3 and the length is 3
Traceback (most recent call last):
File "C:\Users\KILIAN\Documents\english.py", line 10, in <module>
print("i is egal to ", l, " and the length is", word, "\n \n")
~~~~^^^
IndexError: list index out of range


---

if word != chain:
i += 1

I added this if so that the iteration is not done on i += 1 when the word is found

---

For the "while" I originally wrote while word =! chain but that didn't work either

If you give me the solution it would of course be great, but if you have the patience to give me my clues, to put me on the path with explanations so that I can fully understand on my own, it would be really incredible!

Thanks.

[i]Incidentally very strange but the only password that does not return an error is "az"
Reply
#13
That is not the output from the posted code. Your code raises an exception when it does this:
for l in list:
Python has a built-in function named "list", but it is not an iterator. Did you have a variable named "list" that you didn't include in the post?

Another error is that word will never equal chain. word is a list. chain is a string. A string will never equal a list. A logic error like this could be why you get an index error.

list is a bad variable name because it prevents your program using the list function. In your program the list function could be used to replace this:
word = input("Enter your password : ")
word = [caractere for caractere in word]
with this:
word = list(input("Enter your password : "))
Another bad variable name is "l" (lowercase L). It looks like "1" (the digit one). You should avoid "O" (uppercase o) for the it's similarity to zero.
Reply
#14
Thanks for your answer deanhystad. I actually forgot the first line, sorry.

Thank you for your very clear explanations, I already understand better some of my errors.

By the way if it can help I also added the very strange information that the only password that does not return an error is "az"
Reply
#15
Any password ending with z will work. This is because you increment "i" when you find a match. If the last letter is not "z", the for loop continues after the match, and "i" is out of range.
Reply
#16
What can you recommend me to fix these errors?
I believe that one of them as you say is that when for example word[0] is found, the for loop continues instead of starting from 0
Reply
#17
I don't know how to fix your program because I really don't know what your program is supposed to do. Could you describe please? I'm pretty sure the solution won't require indexing.
Reply
#18
I want to create my own brute force tool (just to see if I would succeed, not for malicious purposes)
So I started with the characters [a-z]
Reply
#19
Sorry, but that description doesn't mean anything to me. Tool to do what? Crack a password? Your algorithm has no hope of doing anrthing like that. Where is thete any encription?
Reply
#20
My project is to develop my Python programming skills by recreating a concept of bruteforce on a small scale. I want to practice writing code using a for loop to test all possible combinations of alphabetic characters to find a predefined password. It is important to note that my intention is in no way to create an actual bruteforce tool or violate the privacy of other users. I only work on my own password, which is entered in plain text and is not encrypted. I am aware that my program can neither decrypt nor encrypt anything, and that there is no notion of hash in my project. I'm a hobbyist just looking to get familiar with the Python language and practice programming by simulating a small-scale bruteforce process.
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,419 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,929 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,363 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,450 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,593 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,233 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,864 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,326 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,580 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