Reputation: 5
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
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:
length
variable is being overwritten with two overlapping
statements length = int(...)
and length = len(password)
.characters
as an iterable and an iteration across the
range function.Upvotes: 1