dimanche 7 juin 2020

Python Random: IndexError: string index out of range

I have searched for this error and found multiple people posting the same thing. I cannot however relate it to my issue. Some of the other questions are: Python error: "IndexError: string index out of range" i get ths error : string index out of range IndexError: string index out of range python Python IndexError: string index out of range String index out of range - Python

The piece of code i'm specifically having trouble with is this:

pw_gen = ""
def generate():
    global pw_gen
    if lower == 1: pw_gen = string.ascii_lowercase
    if upper == 1: pw_gen = pw_gen + string.ascii_uppercase
    if number == 1: pw_gen = pw_gen + string.digits
    if special == 1: pw_gen = pw_gen + string.punctuation
    print("".join(random.choices(pw_gen, weights=None, cum_weights=None, k=15)))

Unfortunately when I run this code I receive the error in the title. Any help with correcting this would be highly appreciated. It is also worth mentioning. lower, upper, number and special are all tkinter.IntVar(). Setting pw_gen as a global variable did not solve my problem.

Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire