Home > Back-end >  "i is not accessed Pylance". the "i" in for loop doesn't work
"i is not accessed Pylance". the "i" in for loop doesn't work

Time:10-23

The variables in all of the following for loops says that "variable is not accessed Pylance" The font color of the variable is "dead".

if special == "n" and numbers == "n":
            for i in range(l):
                password.append(random.choice(chars))
        elif special == "y" and numbers == "y":
            for c in range(l - nos - non):
                password.append(random.choice(chars))
            for s in range(nos):
                password.append(random.choice(special))
            for n in range(non):
                password.append(random.choice(numerics))

Full code

import random

chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV"
special = ["@", "#", "$", "%", "&", "*", "."]
numerics = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
password = []

def length(): #ask length
    while True:
        length = int(input("Enter length: "))
        if length > 0:
            return length
        else:
            print("Too short")

def spec(): #ask if want speci char
    while True:
        needsSpecial = input("Do you need special chracter? (y/n): ")
        if needsSpecial == 'y' or needsSpecial == 'n':
            return needsSpecial

def numofspecial(): #ask amount of speci char
    while True:
        specialLen = int(input("How many special charcters?: "))
        if specialLen < 1 or specialLen > l:
            print("wrong input")
        else:
            return specialLen


def num(): #ask if want num
    while True:
        needsNum = input("Do you need numbers? (y/n): ")
        if needsNum == 'y' or needsNum == 'n':
            return needsNum
def numofnum(): #ask for # of num
    while True:
        numofnum = int(input("How many numbers?: "))
        if numofnum < 1 or numofnum > l or (specials == "y" and numofnum > (l - nos)): 
            print("wrong input")
        elif l - nos == 0: 
            return numofnum
        else:
            return numofnum

def main():
    password = []
    global l 
    l = length()
    if l == 1:
        lst = [chars, special, numerics]
        password.append(random.choice(random.choice(lst)))
        password = ''.join(str(e) for e in password)
        print(password)
    else:
        global specials
        specials = spec()
        if specials == "y":
            global nos
            nos = numofspecial()
        numbers = num()
        if numbers == "y":
            global non
            non = numofnum()

        if special == "n" and numbers == "n":
            for i in range(l):
                password.append(random.choice(chars))
        elif special == "y" and numbers == "y":
            for c in range(l - nos - non):
                password.append(random.choice(chars))
            for s in range(nos):
                password.append(random.choice(special))
            for n in range(non):
                password.append(random.choice(numerics))
        print(password)
        random.shuffle(password)
        password = ''.join(str(i) for i in password)
        print(password)
main()

CodePudding user response:

As was mentioned in the comments, the reason for the warning is that you don't use the variable; the convention in python is to use _ for such variables. You can get rid of this problem by using random.choices, passing it a k value of the number of random values you want. I've modified your code to simplify it; note this depends on both non and nos being initialised to 0 and modified (not defined) if the user wants numeric or special characters:

nos = 0
specials = spec()
if specials == "y":
    nos = numofspecial()
non = 0
numbers = num()
if numbers == "y":
    non = numofnum()
# ...
noc = l - nos - non
password = []
password  = random.choices(chars, k=noc)
password  = random.choices(special, k=nos)
password  = random.choices(numerics, k=non)
# make a string
password = ''.join(random.sample(password, k=l))

Notes

  • you can simplify the string password generation using random.sample
  • you had a typo in your if, you should be use specials, not special. However with this code change it is no longer an issue.
  • Related