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 <module>
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

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()

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

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

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

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.

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!

This article has been dead for over six months. Start a new discussion instead.