kuzar

DailyProgrammer #4 Easy

Jun 29th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #Password generator
  2.  
  3. import random
  4.  
  5. lower = True
  6. upper = False
  7. num = True
  8. special = False
  9. space = False
  10.  
  11. print("Take note of syntax. Uppercase letter denotes default value used if input is invalid/blank. \nHitting ENTER repeatedly yields one (1) password with eight (8) lowercase alphanumeric characters.", end="\n\n")
  12.  
  13. # Obtain parameters from user
  14.  
  15. print("Will your password contain...", end="\n\n")
  16.  
  17. if str(input("lowercase letters? (Y/n): ")).lower() == 'n':
  18.     lower = False
  19.  
  20. if str(input("UPPERCASE LETTERS? (y/N): ")).lower() == 'y':
  21.     upper = True
  22.    
  23. if str(input("NUM83R5? (Y/n): ")).lower() == 'n':
  24.     num = False
  25.  
  26. if str(input("$PEC!@|_ C|-|@R@CTER$? (y/N): ")).lower() == 'y':
  27.     special = True
  28.  
  29. if str(input("S p a c e s ? (y/N): ")).lower() == 'y':
  30.     space = True
  31.    
  32. length = input("How long will this password be? (Default=8) ")
  33.  
  34. try:
  35.     length = int(length)
  36. except ValueError:
  37.     length = 8
  38.    
  39. times = input("How many passwords do you want? (Default=1) ")
  40.  
  41. try:
  42.     times = int(times)
  43. except ValueError:
  44.     times = 1
  45.    
  46. # Create sample space
  47.  
  48. alpha_lower = ['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']
  49.  
  50. alpha_upper = ['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']
  51.  
  52. numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  53.  
  54. special_char = ['.', ',', ';', ':',"'",'"', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '[', ']', '{', '}', '<', '>', '/', '=', '\\', '?', '+', '|', '`', '~']
  55.  
  56. space_char = [' ']
  57.  
  58. sample_space = []
  59.  
  60. if lower:
  61.     sample_space = sample_space + alpha_lower
  62.    
  63. if upper:
  64.     sample_space = sample_space + alpha_upper
  65.    
  66. if num:
  67.     sample_space = sample_space + numbers
  68.  
  69. if special:
  70.     sample_space = sample_space + special_char
  71.    
  72. if space:
  73.     sample_space = sample_space + space_char
  74.  
  75. sample = ''.join(sample_space)
  76.  
  77. # Generate password
  78.  
  79. if times > 1:
  80.     print("Here are your passwords:", end="\n")
  81. else:
  82.     print("Here is your password:", end="\n")
  83.  
  84.  
  85.  
  86. while times > 0:
  87.     password = []
  88.     size = length
  89.     while size > 0:
  90.        
  91.         password.append(random.choice(sample))
  92.         size -= 1
  93.    
  94.     print(''.join(password), end='\n')
  95.     times -= 1
Add Comment
Please, Sign In to add comment