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()
CodePudding user response:
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 statementslength = int(...)
andlength = len(password)
. - You are using both
characters
as an iterable and an iteration across the range function.