"Wolf Halton" <wolf.hal...@gmail.com> wrote
Is there a less clunky way to do this?
def new_pass():
series = ['`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'-',
'=', \
'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'_',
'+', \
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[',
']',
'\\', \
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{',
'}',
'|', \
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', "'",
\
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
\
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', \
'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?']
You don't need a list here a simple string would
do and be easier to type and use. You can index
a string as easily as a list.
passwd = []
p = input("Enter the length you want your password to be: ")
# length of password
for i in range(p):
r = random.randint(0, 94)
passwd.append(series[r]) # Append a random char from series[]
to
passwd
You could do this with a list comprehension and using
the choice() function from the whrandom module.
passwd = [whrandom.choice(series) for n in range(p)]
print "".join(map(str, passwd)), " is your new password. \n"
You might want to return the string rather than print it.
Then you can print with
new_p = new_pass()
print new_p, 'is your new password'
I'd also move the length question out of the function
and make it a parameter so your final code looks like:
ln = raw_input("Length of password?")
np = new_pass(ln)
print np, 'is your new password'
For extra points make the alphabet a parameter with
a default value so you can use it for non latin alphabets
too...
klingon = "......."
ln = raw_input(klingon_phrases['length'])
np = new_pass(ln, klingon)
print np, klingon_phrases['new']
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor