0

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()
3
  • 3
    the length variable is being set to zero with length = len(password) Commented Sep 1, 2022 at 21:45
  • You are printing (prefixes of?) the password, not returning anything. Commented Sep 1, 2022 at 21:56
  • You aren't saving the return value of "".join to do anything with it, and characters is just an integer, not a character, in the body of the loop. In short, virtually nothing about the code after the call to random.shuffle is useful. Commented Sep 1, 2022 at 21:58

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.