ayoub03
ayoub03

Reputation: 5

password generator code returns empty output

password generator returns empty password list after using join and for-loop

import random
import string


def generate_random_password():
    characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")
    length = int(input("Enter password length: "))
    password = []
    length = len(password)
    random.shuffle(characters)
    for characters in range(length):
        "".join(characters)
        password.append(characters)
        print(password)


generate_random_password()

Upvotes: 0

Views: 55

Answers (1)

Samuel Montoya Serna
Samuel Montoya Serna

Reputation: 26

I would suggest the following approach (which looks kind of the same as your approach but with some variations):

import random
import string

def generate_random_password():
    characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")
    length_selected = int(input("Enter password length: "))
    random.shuffle(characters)
    return "".join(characters[:length_selected])
    
generate_random_password()

Explanation:

  • The length variable is being overwritten with two overlapping statements length = int(...) and length = len(password).
  • You are using both characters as an iterable and an iteration across the range function.

Upvotes: 1

Related Questions