Created
May 14, 2017 12:07
-
-
Save odanga94/8dc122fd45e9f22ca11925ce9b38d256 to your computer and use it in GitHub Desktop.
PracticePython/Exercise16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
lower_case = set(string.ascii_lowercase) | |
upper_case = set(string.ascii_uppercase) | |
digits = set(string.digits) | |
special_char = set(string.punctuation) | |
def password_generator(): | |
length = int(raw_input("How long a password do you want (at least 4)? ")) | |
password = set(random.sample((lower_case | upper_case | digits | special_char), length)) | |
while password.isdisjoint(lower_case) or password.isdisjoint(upper_case) \ | |
or password.isdisjoint(digits) or password.isdisjoint(special_char): # this code ensures the password contains at least one character from each sub-set. | |
password = set(random.sample((lower_case | upper_case | digits | special_char), length)) | |
else: | |
password_list = list(password) | |
password_string = "".join(password_list) | |
print("Your new password is: %s") %(password_string) | |
password_generator() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment