-3

So i'm trying to create a password generator. It's pretty simple right now but I want it to be able to make and guess passwords without any limits. Could you look at my code and possibly tell me what i'm doing wrong. Everytime I run my code the compiler keeps saying that "list index is out of range." I also don't think my random function is set up properly.

from random import *
import os
u_pwd = input("Enter a password: ")
pwd = ['0','1','2','3','4','5','6','7','8','9']
pw = 0
outcomes = 10 ** len(u_pwd)
i = 0
possible = list(range(0, 10 ** (len(u_pwd))))

while(pw != int(u_pwd)):
    for letter in range(len(u_pwd)):
        i = randint(0,len(possible))
        pw = possible[i]
        print(pw)
        print("Cracking Password..... Please wait..... ")
        print("Attempt ",i, " of ", outcomes)
        os.system("cls")

print("Your password is : ",pw)
if i < 2:
    print("This password took ",i," attempt")
else:
    print("This password took ",i," attempts")

Any help would be appreciated.

I've tried getting the computer to count from 0 all the way to highest possible number given the lenght of the passowrd that the user puts in. That hasn't worked and i figured it was impracticle considering randomly picking a password is also an option.

  • You can try `pw = possible[i % len(possible)]`. – Aqib Chattha Aug 31 '23 at 22:27
  • 1
    It should tell you where in the code it's going wrong. So look at what you're doing on that line, and then start debugging: what values does the list index take each iteration? – Mike 'Pomax' Kamermans Aug 31 '23 at 22:30
  • `randint()` should be `randrange()` – Barmar Aug 31 '23 at 22:43
  • You could also just use `random.choice()` to pick a random element of the list. – Barmar Aug 31 '23 at 22:44
  • `i` isn't the number of attempts. It's just a random list index. Did you have a previous version that went through the list sequentially? – Barmar Aug 31 '23 at 22:45
  • 1
    Welcome to Stack Overflow. "Could you look at my code and possibly tell me what i'm doing wrong." - that isn't generally how the site works; we do not find the bug for you here; we require a **specific** question - which will come out of your best attempt to [understand](//meta.stackoverflow.com/q/261592/) and [locate](//ericlippert.com/2014/03/05) a specific problem, and showcase it in a [mre]. Please read [ask] for more details. – Karl Knechtel Aug 31 '23 at 22:50
  • the reason why they have invented debuggers is that you can find the problem in your code and not have to wait till somebody else has time to fix your problem – rioV8 Aug 31 '23 at 22:51
  • @KarlKnechtel "we do not find the bug for you here" -- is that really true? – John Gordon Aug 31 '23 at 22:53
  • @JohnGordon we expect "debugging questions" to involve a prior attempt at debugging, such that the problem is readily apparent; and we expect a question that is directly about, and specific to, that problem, yes. Otherwise questions should be closed as "needs more focus" or "needs debugging details", unless the problem is recognizable as handled by a canonical duplicate (as I have offered here). Please have a look through meta.SO to understand in more detail. – Karl Knechtel Aug 31 '23 at 22:56

1 Answers1

-1

This is the problem:

i = randint(0,len(possible))
pw = possible[i]

The arguments that you pass to randint() are inclusive, meaning the returned number can be anywhere in that range, including the lowest and highest numbers that you passed in.

So if the input password was, say, three digits, then the length of possible would be 1000, and randint() could return any number from 0 to 1000.

But Python lists are zero-indexed, meaning that the actual valid indexes in the possible list are only 0-999. 1000 is out of range.

So if you get a bit unlucky and randint() happens to choose exactly 1000, you will get the error.

To fix this, use a number that is one smaller in the call to randint():

i = randint(0, len(possible)-1)
John Gordon
  • 29,573
  • 7
  • 33
  • 58