do/while structure needed
Paul McGuire
ptmcg at austin.rr._bogus_.com
Sun May 14 14:26:13 EDT 2006
"Ten" <runlevelten at gmail.com> wrote in message
news:mailman.5677.1147617486.27775.python-list at python.org...
> On Sunday 14 May 2006 06:17, John Salerno wrote:
> > 1 random.shuffle(letters)
> > 2 trans_letters = ''.join(letters)[:len(original_set)]
> > 3 trans_table = string.maketrans(original_set, trans_letters)
> >
> > So what I'd like to do is have lines 1 and 2 run once, then I want to do
> > some comparison between original_set and trans_letters before running
> > line 3. If the comparison passes, line 3 runs; otherwise, lines 1 and 2
> > run again.
> >
> > A do/while would be good for this, but perhaps I'm looking at it in the
> > wrong way? Or is there some kind of do/while type of idiom that I could
> > use?
> >
> > Thanks.
>
> while not comparison(original_set, trans_letters):
> random.shuffle(letters)
> trans_letters = ''.join(letters)[:len(original_set)]
>
> trans_table = string.maketrans(original_set, trans_letters)
>
I don't think the OP wants to call comparison until after the first pass
through the loop. Here's a modification to your version that skips the
comparison test on the first pass:
first = True
while first or not comparison(original_set, trans_letters):
first = False
random.shuffle(letters)
trans_letters = ''.join(letters)[:len(original_set)]
trans_table = string.maketrans(original_set, trans_letters)
-- Paul
More information about the Python-list
mailing list