Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Posted on pastebin https://pastebin.com/9CmCsNnq. Random password randomly generates a password of any required length. Coded by ken Adams in Python. The characters to make the password are the small letters , capital letters and numbers all contained in the one list 'password_characters' .The function 'make_password(n)' is called where 'n' is the length of password required. I generate three separate lists for small letters, capital letters and numbers using their ASCII codes then combines these list together. I learned how to do this from this website:reference:https://terrameijar.wordpress.com/2017/02/03/python-how-to-generate-a-list-of-letters-in-the-alphabet/. It may have been just as easy to type the long list everything out instead of using coding (however I think the process is instructive). At The bottom of this coding there is another function 'make_password2(n)' that takes that approach; it uses a list called 'neat_list'.
- '''
- from random import choice
- ################################################## building the list of characters ########################
- # making a list of the alphabet in lower case
- # the ASCII code for 'a' ia 97 and for 'z' is 122
- #print(ord('a')) # prints 97
- alphabet = []
- for letter in range(97,123):
- alphabet.append(chr(letter))
- ##print(alphabet) # for testing
- # making a list of the alphabat in upper case
- # the ASCII code for 'A' ia 65 and for 'Z' is 90
- Alphabet = []
- for letter in range(65, 91):
- Alphabet.append(chr(letter))
- #print(Alphabet) # for testing
- # the ASCII code for '0' ia 4 and for '9' is 57
- digits = []
- for digit in range(48,58):
- digits.append(chr(digit))
- ##print(digits) #for testing
- # we combine all these lists to make the list from which we will randonly select characters
- password_characters = alphabet + Alphabet + digits
- ##print(password_characters) # for testing
- ################################################### end of building the list of characters ###############
- #################################################### making a password randomly from the list #############
- # now we can make a password
- print("\nUsing password_characters")
- def make_password(n):# n will be the number of characters in the password
- password = "" # start with an empty string
- for x in range(n): #loop n times
- # randomly choose a character and append it to the password list
- password = password + str(choice(password_characters))
- return password # return the completed password of length n
- n = int(input("How long do you want your password to be, how many characters? " ))
- my_new_password = make_password(n) # passes the parameter 'n' and the password is returned
- print("Your new password is " + my_new_password)
- #################################################### Code when the list of characters is already generated####
- print("\nNow using neat_list")
- neat_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
- 'v', 'w','x', 'y', 'z' , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
- 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
- #This is really the same function as above using 'neat_list' instead of 'password_characters'
- def make_password2(n):# n will be the number of characters in the password
- password = "" # start with an empty string
- for x in range(n): #loop n times
- # randomly choose a character and append it to the password list
- password = password + str(choice(neat_list))
- return password # return the completed password of length n
- n = int(input("\nHow long do you want your password to be, how many characters? " ))
- my_new_password = make_password2(n) # passes the parameter 'n' and the password is returned
- print("Your new password is " + my_new_password)
- print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement