while and if

Satchidanand Haridas sharidas at zeomega.com
Fri Aug 13 08:40:44 CEST 2004


Calvin79 wrote:

>Hi Satchit,
>
>Thankyou for what you've already done, but would you mind if I ask one
>last thing and then I'll leave you alone?
>
>In it's present form the output is fine, but I think I might like to have
>it output in this format;['c', 'b', 'a', 'd']. It is probably the most
>basic of questions to ask, but heck I've only been at this python stuff a
>week... : )  
>
>Calvin
>
>  
>
Hi,

Change the 'print' line to append to a list:

<code>

import random

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

state = [None,None]

l = []                                                      # new line

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        
    l.append(tmp)                                    # new line
    state[x%2] = tmp

print l                                                     # new line

</code>

----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open  Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India






More information about the Python-list mailing list