[Tutor] Clunky Password maker
Noah Hall
enalicho at gmail.com
Wed May 25 19:55:24 CEST 2011
On Wed, May 25, 2011 at 6:25 PM, Wolf Halton <wolf.halton at gmail.com> wrote:
> Is there a less clunky way to do this?
> [code]
> 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', '<', '>', '?']
> 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
> #print passwd
> #print passwd[0], passwd[1], passwd[2], passwd[3]
> print ""
> print "".join(map(str, passwd)), " is your new password. \n"
> [/code]
I suggest you read up on the random module - there's two things that
you'll find immediately useful - random.choice and random.sample. I
suggest you use sample instead of the for loop you're using. Also,
when you print the output, there's no need to use map - everything in
your series.
More information about the Tutor
mailing list