mardi 22 janvier 2019

Empty variable in for-loop

I have this one-liner which works entirely fine.

import random
from string import ascii_letters, digits

def pwd_generator(pwd):

    password = "".join([random.choice(ascii_letters + digits) for i in range(pwd)])
    print(password)

I wanted to translate that now into a 'normal' for-loop.

def pwd_generator(pwd):
    password = ''
    for i in range(pwd):
        password.join([random.choice(ascii_letters + digits)])

    print(password)

In this scenario however, password is empty.

Why is the variable empty when I try to write it in a 'proper' for-loop?




Aucun commentaire:

Enregistrer un commentaire