Advertisement
kenadams53

Random Password

Aug 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. '''
  2. 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'.
  3. '''
  4.  
  5.  
  6. from random import choice
  7.  
  8. ################################################## building the list of characters ########################
  9. # making a list of the alphabet in lower case
  10. # the ASCII code for 'a' ia 97 and for 'z' is 122
  11. #print(ord('a')) # prints 97
  12. alphabet = []
  13. for letter in range(97,123):
  14. alphabet.append(chr(letter))
  15. ##print(alphabet) # for testing
  16.  
  17. # making a list of the alphabat in upper case
  18. # the ASCII code for 'A' ia 65 and for 'Z' is 90
  19. Alphabet = []
  20. for letter in range(65, 91):
  21. Alphabet.append(chr(letter))
  22. #print(Alphabet) # for testing
  23.  
  24. # the ASCII code for '0' ia 4 and for '9' is 57
  25. digits = []
  26. for digit in range(48,58):
  27. digits.append(chr(digit))
  28. ##print(digits) #for testing
  29.  
  30. # we combine all these lists to make the list from which we will randonly select characters
  31. password_characters = alphabet + Alphabet + digits
  32. ##print(password_characters) # for testing
  33.  
  34. ################################################### end of building the list of characters ###############
  35.  
  36.  
  37. #################################################### making a password randomly from the list #############
  38. # now we can make a password
  39. print("\nUsing password_characters")
  40. def make_password(n):# n will be the number of characters in the password
  41. password = "" # start with an empty string
  42. for x in range(n): #loop n times
  43. # randomly choose a character and append it to the password list
  44. password = password + str(choice(password_characters))
  45. return password # return the completed password of length n
  46.  
  47. n = int(input("How long do you want your password to be, how many characters? " ))
  48. my_new_password = make_password(n) # passes the parameter 'n' and the password is returned
  49. print("Your new password is " + my_new_password)
  50.  
  51. #################################################### Code when the list of characters is already generated####
  52. print("\nNow using neat_list")
  53. neat_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
  54. 'v', 'w','x', 'y', 'z' , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  55. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  56.  
  57. #This is really the same function as above using 'neat_list' instead of 'password_characters'
  58. def make_password2(n):# n will be the number of characters in the password
  59. password = "" # start with an empty string
  60. for x in range(n): #loop n times
  61. # randomly choose a character and append it to the password list
  62. password = password + str(choice(neat_list))
  63. return password # return the completed password of length n
  64.  
  65. n = int(input("\nHow long do you want your password to be, how many characters? " ))
  66. my_new_password = make_password2(n) # passes the parameter 'n' and the password is returned
  67. print("Your new password is " + my_new_password)
  68. print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement