1 Replies - 523 Views - Last Post: 29 March 2010 - 10:18 PM

#1 Jessehk   User is offline

  • New D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 24
  • Joined: 06-January 06

Password Generator

Posted 25 February 2006 - 10:30 AM

Description: Generate a password of given length
import random

class Password:
     possible = tuple([str(x) for x in range(10)] + 
                       [let for let in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'] + 
                         [symbol for symbol in '/<>[email protected]#$%^()'])

     def __init__(self, number_of_letters):
          try:
               self.generate(number_of_letters)
          
          except ValueError, ve:
               raise ve
          
     def get_password(self):
          return "".join(self.password)

     def generate(self, length):
          if length < 0:
               raise ValueError("Invalid length specified.")
          else:
               self.length = length
               self.password = []

               for letter in range(self.length):
                    self.password.append(random.choice(self.possible))
def main():
     try:
          p = Password(int(raw_input("Enter password length: ")))
          print "Random password: %s" % p.get_password()
     
     except ValueError, ve:
          print ve

if __name__ == '__main__':
     main()



Is This A Good Question/Topic? 0
  • +

Replies To: Password Generator

#2 klue_90   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 10

Re: Password Generator

Posted 29 March 2010 - 10:18 PM

Doesn't work for Python 3.1.1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1