I know Remembering secure passwords is difficult. So I made this.

import random, os
a= ['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','1','2','3','4','5','6','7','8','9','{',':','>','?','}','|','!','@','#','$','%','^','&','*','(',')','-','+']

z = int(raw_input("How many words in your password?: "))
os.system("clear")
List = []
for x in xrange(z):
	List.append(random.choice(a))
print "".join(List) 
print "1) Yes"
print "2) No"
b = int(raw_input("Would you like to save this password to a text file?: "))
if b == 1:
	c = raw_input("What is this password for?: ")
	x = open("%s.txt" % c, "w")
	y = "".join(List)
	x.write(y)
	x.close
	x = open("%s.txt" % c, "r")
	x.read
	print "Saved!"
elif b == 2:
	raw_input("Press Enter To Continue")

Recommended Answers

All 8 Replies

I created a Random Password Generator awhile back as well. Mine how ever has a GUI. You can find the complete program at CoderProfile.

#!/usr/bin/env python
    
#----------------------------------------------------------------------
# randompass.py
# Author: Shady Tyrant
# 07/20/2009
#----------------------------------------------------------------------
 
import sys
import random
from GladeWindow import *
 
#----------------------------------------------------------------------
 
class randompass(GladeWindow):
 
    #----------------------------------------------------------------------
 
    def __init__(self):
 
        ''' '''
        
        self.init()
 
    #----------------------------------------------------------------------
 
    def init(self):
 
        filename = 'randompass.glade'
 
        widget_list = [
            'window1',
            'charNumEntry',
            'genButton',
            'clearButton',
            'passEntry',
            ]
 
        handlers = [
            'on_genButton_clicked',
            'on_clearButton_clicked',
            ]
 
        top_window = 'window1'
        GladeWindow.__init__(self, filename, top_window, widget_list, handlers)
    #----------------------------------------------------------------------
 
    def on_genButton_clicked(self, *args):
           # Creates tuple of chars used, 71 total
           mystring = ("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", "!", "@", "#", \
               "$", "%", "^", "&", "*", "_")
               
        try:
               num = int(self.widgets['charNumEntry'].get_text())
               if num >= 72:
                            self.widgets['passEntry'].set_text('ERROR: length to large')
           except:
               self.widgets['passEntry'].set_text('ERROR: Failed Recieving Length Value')
        
           # Creates random pass and prints to textbox
           rPass = ''.join(random.sample(mystring, num))
           self.widgets['passEntry'].set_text(rPass)
           
    #----------------------------------------------------------------------
 
    def on_clearButton_clicked(self, *args):
        self.widgets['passEntry'].set_text('')
        self.widgets['charNumEntry'].set_text('')
 
#----------------------------------------------------------------------
 
def main(argv):
 
    w = randompass()
    w.show()
    gtk.main()
 
#----------------------------------------------------------------------
 
if __name__ == '__main__': main(sys.argv)

I know Remembering secure passwords is difficult. So I made this.

import random, os
a= ['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','1','2','3','4','5','6','7','8','9','{',':','>','?','}','|','!','@','#','$','%','^','&','*','(',')','-','+']

z = int(raw_input("How many words in your password?: "))
os.system("clear")
List = []
for x in xrange(z):
	List.append(random.choice(a))
print "".join(List) 
print "1) Yes"
print "2) No"
b = int(raw_input("Would you like to save this password to a text file?: "))
if b == 1:
	c = raw_input("What is this password for?: ")
	x = open("%s.txt" % c, "w")
	y = "".join(List)
	x.write(y)
	x.close
	x = open("%s.txt" % c, "r")
	x.read
	print "Saved!"
elif b == 2:
	raw_input("Press Enter To Continue")

Nice effort!

My comment, I see you are still using tabs for indentations. Avoid them and use 4 spaces as most other folks. If you use tabs, you sooner or later will mix them with spaces, then your code becomes a mess. Also, if you want to use the 2to3.py utility to convert your code for Python3 later on, it will kick you!

Also avoid os.system("clear"), it is simply not needed and is not at all portable to other systems.

Instead of manually writing out all the letters and digits do this:

import string
 a = list(string.ascii_letters + string.digits + "!@#$%^&*()-+={}|\/?><,.'")

Instead of manually writing out all the letters and digits do this:

import string
 a = list(string.ascii_letters + string.digits + "!@#$%^&*()-+={}|\/?><,.'")

Oh thats a neat piece of code. I didn't know you could do that.

I actually learned that when I was making a random Password Generator myself :).
BTW, That piece of code includes uppercase and lowercase letters. If you want just lower case letters you can use "string.ascii_lowercase" instead of "string.ascii_letters", or if you want all uppercase letters you can do "string.ascii_uppercase".

Also the OP didn't want a zero in the digits part. :)

Okay, instead use:

import string
digits  = "".join([loop for loop in range(1, 10)])
 a = list(string.ascii_letters + digits + "!@#$%^&*()-+={}|\/?><,.'")

Okay, this might even be mildly better:

import string
a = list(string.ascii_lowercase + "!@#$%^&*()-+={}|\/?><,.'") + range(1, 10)

print a

For Python3 change to list(range(1, 10)). Oh, the joys of Python! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.