954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random Password maker-help please!

Hey guys..

I have been making a random password maker, which makes a password of 8 characters length, but I am encountering a problem.

Here'e the code I used:

import random

list=['a','b','c','d','e','f','g','h','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!','@','#','$','%','^','&','*']

randlist=''
a=1

def password():
    while a<=8:
        randlist+=random.choice(list)
        a+=1
    return

password()

print randlist

print 'Are you satisfied with your password?'
take=raw_input("YES or NO: ")

if take=='YES':
    quit
elif take=='NO':
    password()


And here's the error I encounter:Traceback (most recent call last):
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\password.py", line 14, in
password()
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\password.py", line 9, in password
while a<=8:
UnboundLocalError: local variable 'a' referenced before assignment

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

It's only a small error but what you want to do is this:

import random

list=['a','b','c','d','e','f','g','h','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!','@','#','$','%','^','&','*']



def password():
    a = 0 # a needs to be in the function, also when it was set as 1 it would only produce 7 character passwords
    randlist=''
    while a<=8:
        randlist+=random.choice(list)
        a+=1
    print randlist

password()



print 'Are you satisfied with your password?'
take=raw_input("YES or NO: ")

if take=='YES':
    quit
elif take=='NO':
    password()
leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

Thanks a lot, it worked! Is it compulsory to add the return statement?

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

No, I don't know why but it said that randlist isn't an item outside of the function, so I just added it into it

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

Hahaha, for things like this, i always try and do list comprehensions and get it down to one line, that is not including any of the import statements.

I got this

import random
values = 'abcdefghjklmnopqrstuvwxyz!@#$%^&*'
print ''.join([random.choice(value) for g in range(8)])

So what that does is it uses list comp to get a random letter from the values variable. it does this eight times, as many as are in the range. Then once that is done using the join function i join all of the list together into one simple eight digit long string ready for printing or storing as a variable!

Hope that helps, or is slightly interesting :P

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

Woho! You make it look soo simple! Kudos mann! Thanks a lot! That really got me fired up! You are sick!

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

Paulthom's solution is great!

That having been said, the error message you were getting was correct. You defined your loop variable outside the function; so when you tried to use the variable within your function, it had no idea what you were talking about.

PetuniaRose
Light Poster
25 posts since Jun 2009
Reputation Points: 23
Solved Threads: 1
 

Hold on, i just realised, its not one line... And i know python isnt about making things as small as possible. Its just i love the challenge of doing cool list comps :P

import random
print ''.join([random.choice('abcdefghjklmnopqrstuvwxyz!@#$%^&*') for g in range(8)])


Cause i can :)

P.s. Imports dont count!

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You