vendredi 17 avril 2020

name 'randrange' is not defined ERROR, password generator

I'm trying to code a password generator but it's not working and I can't understand the error.

import random
import string

lw = list(string.ascii_lowercase)
uw = list(string.ascii_uppercase)
ns = list(string.digits)
password = ""

def addLW():
    f = randrange(1, len(lw))
    password = password + lw[f]

def addUW():
    f = randrange(1, len(lw))
    password = password + uw[f]

def addN():
    f = randrange(1, len(lw))
    password = password + ns[f]

funcs = [addLW, addUW, addN]

maxx = input("Password generator.\nMax: ")

if maxx.isdigit():
    maxx = int(maxx)
    for i in range(maxx):
        func = random.choice(funcs)
        func()
    print(f"Password: {password}")
else:
    print("Error")

Full error:

Traceback (most recent call last):
  File "Password Generator.py", line 29, in <module>
    func()
  File "Password Generator.py", line 14, in addUW
    f = randrange(1, len(lw))
NameError: name 'randrange' is not defined

I don't understand because I've already imported 'random'...




Aucun commentaire:

Enregistrer un commentaire