Advertisement
IsenfireLDC

Random Password Generator 1: Simplified

May 4th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import random
  2. import string
  3. import sys
  4.  
  5. possible_chars = string.ascii_letters + string.digits + string.punctuation
  6.  
  7. def nextchar(chars):
  8.     return random.choice(chars)
  9.  
  10. yes_or_no = input("""
  11. Would you like a random password suggestion generated?
  12. Type Yes to continue: """).lower()
  13.  
  14. if yes_or_no == 'yes':
  15.     try:
  16.         pwd_len = int(input('How long do you want your password? '))
  17.     except ValueError:
  18.         sys.exit("You need to enter an integer. Please start the program over.")
  19.  
  20.     if 0 < pwd_len:
  21.         new_pwd = ""
  22.         for _ in range(pwd_len):
  23.             new_pwd += nextchar(possible_chars)
  24.         print("Your new password is:\n" + new_pwd)
  25.  
  26.     else:
  27.         print("I can only generate passwords between 1 and 25 characters long.")
  28.  
  29. else:
  30.     print("Well then, why did you run me?")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement