Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created March 3, 2016 03:13
Show Gist options
  • Save Kwisses/b08a393896db08af8d64 to your computer and use it in GitHub Desktop.
Save Kwisses/b08a393896db08af8d64 to your computer and use it in GitHub Desktop.
[Daily Challenge #4 - Easy] - Password Generator - r/DailyProgrammer
# ***** Daily Challenge #4 - Easy *****
# You're challenge for today is to create a random password generator!
# For extra credit, allow the user to specify the amount of passwords to generate.
# For even more extra credit, allow the user to specify the length of the strings he wants to generate!
# ---------------------------------------------------------------------------------------------------------------------
from random import sample
def password_generator(pw_length, pw_number):
"""Creates x amount of passwords with y amount of length.
Args:
pw_length (int): Length of passwords to be printed.
pw_number (int): Number of passwords to be printed.
Return:
list: A list containing x amount of passwords with y length.
"""
characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()"
output = []
try:
pw_length = int(pw_length)
pw_number = int(pw_number)
while pw_number > 0:
for _ in characters:
password = sample(characters, k=pw_length)
output.append("\nPassword #%s: " % pw_number + ''.join(password))
break
pw_number -= 1
except ValueError:
print("ERROR: Input not valid. Try again.\n")
main()
return output
def main():
length = input("Length of password: ")
number = input("Number of Passwords to generate: ")
generator = password_generator(length, number)
for pw in generator:
print(pw)
if __name__ == "__main__":
main()
@Kwisses
Copy link
Author

Kwisses commented Mar 3, 2016

[Daily Challenge #4 - Easy] from the DailyProgrammer subreddit. Password Generator.

Reference: https://www.reddit.com/r/dailyprogrammer/comments/pm6oj/2122012_challenge_4_easy/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment