Home > Blockchain >  'int' object is not subscriptable - python
'int' object is not subscriptable - python

Time:06-13

I am currently making a password manager that includes a password generator. My thought prosses is that for each character of the given password length the program would randomly take a variable from the "chars" list and then take a random index of the string and add it to the final "password string.

letters_small = "abcdefghijklmnopqrstuvwxyz"
letters_cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nums = "134567890"
special_chars = "!@#$%^&*?><"

chars = [letters_cap, letters_small, nums, special_chars]

Charlist = []

password = ""
len = ""

def generator():
    while True:
        len = input("Enter the length of the password: ")
        break
        for i in range(len):
            Charlist = chars[random.randint(0, 3)]
            password.append(Charlist[0[random.randint(0, len(Charlist[0]))]])

Everytime I try and debug this section it raises this error

 SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
 password.append(Charlist[0[random.randint(0, len(Charlis))]])

I tried to fix it by adding an index in the last line, but that didn't do anything

password.append(Charlist[0[random.randint(0, len(Charlis[0]))]])

CodePudding user response:

You are getting the error because [0[...]]. Here, 0 is an integer, not a list, so, it is not subscriptable. Also, there are many other problems too. like len is a resereved word. When you use it as a variable and try to len(Charlist[0]), it gives error. len is an str when input is taken in it.

Moreover, the break statement and while loops are unnecessary.


Corrected Code

import random

letters_small = "abcdefghijklmnopqrstuvwxyz"
letters_cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nums = "134567890"
special_chars = "!@#$%^&*?><"

chars = [letters_cap, letters_small, nums, special_chars]

Charlist = []

password = ""
passwordLen = ""


def generator():
    passwordLen = input("Enter the length of the password: ")
    passwordLen = int(passwordLen)

    password = ""
    for i in range(passwordLen):
        Charlist = chars[random.randint(0, 3)]
        password  = Charlist[random.randint(0, len(Charlist)-1)]

    print(password)


generator()

CodePudding user response:

Pyguy,

  • You can break up complex/longer statements into multiple simpler ones for cleaner and comprehensible code.
  • Use a debugger(eg: pdb) to debug such errors.

Writing clear code might help in debugging issues quickly. In your case, I believe this might be the issue:

Charlist[0[ -> Charlist[0][

CodePudding user response:

len is bulit in function in python chan

  • Related