[Tutor] 'number' strings error

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 3 Mar 2001 04:25:42 -0800 (PST)


On Fri, 2 Mar 2001, Christopher Peet wrote:

> Thanks very much for the pointers. I've never programmed before and
> this was my first script. I'm surprised how much it changed and how
> much I learned in just a single day...

Very cool!  That's good to hear.


> The code I posted was just a quick sample. Below is the actual script
> I was working on. As you can see it looks quite a bit nicer now and I
> ended up not needing the string module...


> import random
> 
> ll = ['q','w','e','r','t','a','s','d','f','g','z','x','c','v','b']
> rl = ['y','u','i','o','p','h','j','k','l','b','n','m']
> ln = ['1','2','3','4','5']
> rn = ['6','7','8','9','0']
> 
> ll1 = random.choice(ll)
> rl1 = random.choice(rl)
> ll2 = random.choice(ll)
> rn1 = random.choice(rn)
> ln1 = random.choice(ln)
> rn2 = random.choice(rn)
> ll3 = random.choice(ll)
> rl2 = random.choice(rl)
> ll4 = random.choice(ll)
> 
> password = ll1 + rl1 + ll2 + rn1 + ln1 + rn2 + ll3 + rl2 + ll4
> 
> print password


There's another place where we can make things look neater:

> ll = ['q','w','e','r','t','a','s','d','f','g','z','x','c','v','b']

random.choice() works equally well with strings as well as lists.  If we
tweak the definition of ll to:

    ll = 'qwertyasdfgzxcvb'

your code should still have the same effect.  This will make your
definitions of your code characters a little easier to read and write.

Good luck to you!