do/while structure needed

Discussion in 'Python' started by John Salerno, May 14, 2006.

  1. John Salerno

    John Salerno Guest

    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.
    John Salerno, May 14, 2006
    #1
    1. Advertising

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


    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
    George Sakkis, May 14, 2006
    #2
    1. Advertising

  3. John Salerno

    Ten Guest

    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)

    HTH,

    Ten

    --
    There are 10 types of people in this world,
    those who understand binary, and those who don't.
    Ten, May 14, 2006
    #3
  4. John Salerno

    Paul McGuire Guest

    "Ten" <> wrote in message
    news:...
    > 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
    Paul McGuire, May 14, 2006
    #4
  5. John Salerno

    John Salerno Guest

    George Sakkis wrote:

    > 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.
    John Salerno, May 14, 2006
    #5
  6. John Salerno

    Terry Reedy Guest

    "John Salerno" <> wrote in message
    news:4467786c$0$1956$...
    > 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
    Terry Reedy, May 15, 2006
    #6
  7. John Salerno

    John Salerno Guest

    Terry Reedy wrote:
    > "John Salerno" <> wrote in message
    > news:4467786c$0$1956$...
    >> 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.


    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.
    John Salerno, May 15, 2006
    #7
  8. On Mon, 15 May 2006 18:15:39 GMT, John Salerno
    <> declaimed the following in comp.lang.python:

    > 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, May 16, 2006
    #8
  9. Op 2006-05-15, Terry Reedy schreef <>:
    >
    > "John Salerno" <> wrote in message
    > news:4467786c$0$1956$...
    >> 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.


    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.

    --
    Antoon Pardon
    Antoon Pardon, May 16, 2006
    #9
  10. John Salerno

    John Salerno Guest

    Dennis Lee Bieber wrote:

    > 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?
    John Salerno, May 16, 2006
    #10
  11. Dennis Lee Bieber wrote:

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

    --
    Edward Elliott
    UC Berkeley School of Law (Boalt Hall)
    complangpython at eddeye dot net
    Edward Elliott, May 17, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    745
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,205
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    599
  4. Replies:
    9
    Views:
    25,287
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    778
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page