while and if

Satchidanand Haridas sharidas at zeomega.com
Fri Aug 13 07:46:21 CEST 2004

Calvin79 wrote:

>Hi Satchit,
>Can I also ask how could I stop two of the same letters being given i.e.
>aa or dd and also how would I stop the same letter being repeated with
>only one space between i.e. a b a or c a c


You will have to maintain the previous two states to prevent the letters 
repeating within the next two choices. The code below will do something 
like that. I don't know if it is the best solution:


import random

things = xrange(int(raw_input("choose no of things (1-8)? ")))

state = [None,None]

for x in things:
    tmp = random.choice('abcd')
    print state
    while tmp in state[0:2]:
        tmp = random.choice('abcd')
    print "choice ",x+1," is ", tmp
    state[x%2] = tmp



More information about the Python-list mailing list