do/while structure needed

J

John Salerno

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.
 
G

George Sakkis

John said:
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.

I guess you want something like:

while True:
random.shuffle(letters)
trans_letters = ''.join(letters)[:len(original_set)]
if some_compatison(original_set,trans_letters):
trans_table = string.maketrans(original_set, trans_letters)
break


HTH,
George
 
T

Ten

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)

HTH,

Ten
 
P

Paul McGuire

Ten said:
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
 
J

John Salerno

George said:
while True:
random.shuffle(letters)
trans_letters = ''.join(letters)[:len(original_set)]
if some_compatison(original_set,trans_letters):
trans_table = string.maketrans(original_set, trans_letters)
break

Thanks, that looks pretty good. Although I have to say, a do/while
structure is the much more obvious way. I wonder why it hasn't been
added to the language.
 
T

Terry Reedy

John Salerno said:
Thanks, that looks pretty good. Although I have to say, a do/while
structure is the much more obvious way. I wonder why it hasn't been
added to the language.

Been suggested many times, been considered, and rejected. Easily similated
with while True: ... if exit_condition: break which also solves 'loop and a
half' problem, which is more common. Requires new keyword for little gain.
Etc.

tjr
 
J

John Salerno

Terry said:
Been suggested many times, been considered, and rejected. Easily similated
with while True: ... if exit_condition: break which also solves 'loop and a
half' problem, which is more common. Requires new keyword for little gain.
Etc.

I know, I remember a discussion about it a while back. But IMO, at
least, a do/while structure seems much cleaner and more readable than a
while loop with an embedded if and break statement.
 
D

Dennis Lee Bieber

I know, I remember a discussion about it a while back. But IMO, at
least, a do/while structure seems much cleaner and more readable than a
while loop with an embedded if and break statement.

And what syntax would you propose? Remember, Python uses indentation
for control structures.

Would you put the condition at the top of the loop -- and confuse
those people who believe the exit condition should appear at the point
the exit activates?

until condition:
block
of
many
lines
where
the
condition
is
tested
HERE

do: #no condition
block
of
many
lines
until condition: #start of a new block... but no block follows?
pass?

The basic loop in Ada is:

loop
block;
of;
statements;
end loop;

An exit is implemented by:

loop
block;
of;
exit if condition;
statements;
end loop;

That structure is extended with

for indx in low..high loop
and
while condition loop

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
A

Antoon Pardon

Op 2006-05-15 said:
Been suggested many times, been considered, and rejected. Easily similated
with while True: ... if exit_condition: break which also solves 'loop and a
half' problem, which is more common. Requires new keyword for little gain.
Etc.

It isn't rejected. PEP 315 is only deferred. Which as far as I
understand means it is postponed because other things seemed
more important.
 
J

John Salerno

Dennis said:
And what syntax would you propose?

I guess something like:


do:
stuff goes here
and here
and here
while (condition)


The absence of a colon after the while statement can be the signal that
it isn't a regular while statement with a following block, but instead
the final statement of a do/while loop. Or would that cause an issue?
 
E

Edward Elliott

Dennis said:
Would you put the condition at the top of the loop -- and confuse
those people who believe the exit condition should appear at the point
the exit activates?

Confusion is not the issue. You can put the condition smack dab in the
middle of the loop, as long as the location is consistent people will know
where to look for it. Rejecting because "some are confused the first time
since other languages do it differently" gets you nowhere: some group will
always be confused by something. They learn, problem solved.

I agree that do/while is unnecessary. But I'd argue against it on grounds
of simplicity and readability instead of confusion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top