Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #password.py
- from random import randint, choice, shuffle, sample
- from mycalc import unique
- from time import sleep
- import winsound
- def get_user_choices(special_chars_string):
- print("How many characters long do you want your password to be? Default is 16")
- user_num_chars = input("Enter the number of characters: " )
- if user_num_chars == "":
- user_num_chars = 16
- print("The number of characters was set to 16, by default")
- else:
- user_num_chars = int(user_num_chars)
- print()
- print("These are the special characters available:", special_chars_string)
- print("Or you can make your own list of special characters to use:")
- print("Enter your list of special characters if you want to make your own list")
- print("Enter nothing to accept the default of the list available")
- ans = input("Enter your list or nothing: ")
- if ans != "":
- special_chars_string = unique(ans)
- print("After removing any duplicate characters, your list is", special_chars_string)
- special_chars_list = list(special_chars_string)
- special_chars_list_length = len(special_chars_list)
- user_max_num_special_chars = calc_user_max_num_special_chars(user_num_chars, special_chars_list_length)
- print("Enter the number of special characters. Default is 1. Maximum is", user_max_num_special_chars)
- print()
- user_num_special_chars = input("Enter an integer: ")
- if user_num_special_chars == "":
- user_num_special_chars = 1
- print("The number of special characters was set to 1, by default")
- else:
- user_num_special_chars = int(user_num_special_chars)
- if user_num_special_chars > user_max_num_special_chars:
- user_num_special_chars = user_max_num_special_chars
- winsound.PlaySound("/P31Working/Sounds/beat", winsound.SND_ALIAS)
- limit = user_max_num_special_chars
- print("You asked for too many special characters; the number was reduced to the limit", limit)
- sleep(2)
- return user_num_chars, user_num_special_chars, special_chars_list
- def calc_user_max_num_special_chars(user_num_chars, special_chars_list_length):
- user_max_num_special_chars = user_num_chars - 2
- if user_max_num_special_chars > special_chars_list_length:
- user_max_num_special_chars = special_chars_list_length
- return user_max_num_special_chars
- def select_special_chars_to_add(user_num_special_chars, user_num_chars, special_chars_list):
- return sample(special_chars_list, user_num_special_chars)
- def password(user_num_chars, special_chars_num):
- pw_as_list = []
- # these 2 lines ensure at least 1 alpha and 1 digit
- pw_as_list += choice(all_alpha_list)
- pw_as_list += choice(all_digits_list)
- # get the special characters into pw
- pw_as_list += select_special_chars_to_add(user_num_special_chars, user_num_chars, special_chars_list)
- # add alphanum characters to fill out the pw to the length desired by user
- while len(pw_as_list) < user_num_chars:
- pw_as_list += choice(alphanum_list)
- # shuffle the pw characters
- shuffle(pw_as_list)
- pw = ''.join(pw_as_list)
- return pw
- alphanum_string = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789" # Removed "l", "I", "O"
- alphanum_list = list(alphanum_string)
- # other_chars_string = "!#$%&)(*+/.:;<=>?@^" in Webmaster Password Generator
- special_chars_string = "!$#@%&^*" # These seem to be the only spec. chars permitted by Comcast
- special_chars_list = list(special_chars_string)
- all_alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV"
- all_alpha_list = list(all_alpha)
- all_digits_string = "01234567890"
- all_digits_list = list(all_digits_string)
- print("So you need a password.")
- print()
- while True:
- user_num_chars, user_num_special_chars, special_chars_list = get_user_choices(special_chars_string)
- pw = password(user_num_chars, user_num_special_chars)
- print()
- print("Here are 10 passwords of length", user_num_chars, "that meet your conditions:")
- while True:
- for z in range(10):
- sleep(.2)
- pw = password(user_num_chars, user_num_special_chars)
- print()
- print(pw)
- print()
- print("Another 10 or start over with a different style and number?")
- print("Enter nothing for another 10; 'q' to quit; 'd' to start over.")
- ans = input("Your answer? ")
- print()
- if ans == "":
- print()
- print("Here are another 10 of the same style:")
- print()
- elif ans == "d":
- break
- else:
- break
- if ans == "d":
- print("Starting over:")
- else:
- break
- print("Bye.")
- sleep(2.1)
Add Comment
Please, Sign In to add comment