Making different things equal
wes weston
wweston at att.net
Sun Aug 22 11:46:59 EDT 2004
M. Clift wrote:
> Hi All,
>
> Back again...
>
> This code works nicely giving a list of names with no name repeated at a
> space of 3 i.e. you won't get 'Bob','Bob' or 'Bob','Sue','Bob'.
>
> What I'd like to do is add a condition to it which says that although the
> names are different 'Bob' and 'Mary' are equal and so it won't generate
> 'Bob', 'Rita','Mary'
>
> I've tried various ways to do it, but with no joy. Any help appreciated.
>
> Thanks, M
>
> <code>
>
> from random import *
>
> Name_Number = xrange(int(raw_input("Choose number of Names (1-20)? ")))
>
> state = [None,None]
>
> nextName = {'Bob':['Rita','Sue'],\
> 'Rita':['Mary','Sue','Bob'],\
> 'Sue':['Rita','Mary','Bob'],\
> 'Mary':['Sue','Rita']}
>
> Name_List = []
>
> tmp = choice(('Bob','Rita','Sue','Mary))
>
> for x in Name_Number:
> print state
> while tmp in state[0:2]:
> tmp = choice(nextName[Name_List[-1]])
> print tmp, ", ",
> print
> print "Name ",x+1," is ", tmp
> Name_List.append(tmp)
> state[x%2] = tmp
>
> print Name_List
>
> <code>
>
>
M,
The debugging prints are changed, a tick after Mary is
added so it will run, and a few comments are added.
'still can't figure out what you want to do.
wes
from random import *
Name_Number = xrange(int(raw_input("Choose number of Names (1-20)? ")))
state = [None,None]
nextName = {'Bob':['Rita','Sue'],\
'Rita':['Mary','Sue','Bob'],\
'Sue':['Rita','Mary','Bob'],\
'Mary':['Sue','Rita']}
Name_List = []
tmp = choice(('Bob','Rita','Sue','Mary')) #after Mary ' missing
for x in Name_Number:
print 'state',state
print 'tmp1',tmp
while tmp in state[0:2]: #or for x in range(2):
tmp = choice(nextName[Name_List[-1]])
print 'tmp2',tmp
print "Name ",x+1," is ", tmp
Name_List.append(tmp)
state[x%2] = tmp
print 'Name_List',Name_List
More information about the Python-list
mailing list