Newbie question: Explain this behavior

Discussion in 'Python' started by David Smith, Jul 14, 2005.

  1. David Smith

    David Smith Guest

    Why does code snippet one work correctly, but not two. The only
    difference is the placement of the "else". I know that indentation
    affects execution, but how does it change behavior in the following
    examples? Thank you.

    1. for n in range(2, 10):
    for x in range(2, n):
    if n % x == 0:
    print n, 'equals', x, '*', n/x
    break
    else:
    # loop fell through without finding a factor
    print n, 'is a prime number'


    Output:

    2 is a prime number
    3 is a prime number
    4 equals 2 * 2
    5 is a prime number
    6 equals 2 * 3
    7 is a prime number
    8 equals 2 * 4
    9 equals 3 * 3



    2. for n in range(2, 10):
    for x in range(2, n):
    if n % x == 0:
    print n, 'equals', x, '*', n/x
    break
    else:
    # loop fell through without finding a factor
    print n, 'is a prime number'

    Output:

    3 is a prime number
    4 equals 2 * 2
    5 is a prime number
    5 is a prime number
    5 is a prime number
    6 equals 2 * 3
    7 is a prime number
    7 is a prime number
    7 is a prime number
    7 is a prime number
    7 is a prime number
    8 equals 2 * 4
    9 is a prime number
    9 equals 3 * 3
     
    David Smith, Jul 14, 2005
    #1
    1. Advertising

  2. "David Smith" <> wrote in message news:db6tfq$1kbh$...
    > Why does code snippet one work correctly, but not two. The only
    > difference is the placement of the "else". I know that indentation
    > affects execution, but how does it change behavior in the following
    > examples? Thank you.
    >
    > [snipped]


    In the second example, "else" corresponds to "if", while in the first one it corresponds to "for".
    The tutorial example on "for ... else" loops
    (http://docs.python.org/tut/node6.html#SECTION006400000000000000000) uses the prime number
    computation you posted, so read it again if it's not clear what the "else" clause does with loops.

    George
     
    George Sakkis, Jul 15, 2005
    #2
    1. Advertising

  3. David Smith

    Ross Wilson Guest

    On Thu, 14 Jul 2005 15:46:40 -0700, David Smith wrote:

    > Why does code snippet one work correctly, but not two. The only
    > difference is the placement of the "else". I know that indentation
    > affects execution, but how does it change behavior in the following
    > examples? Thank you.
    >
    > 1. for n in range(2, 10):
    > for x in range(2, n):
    > if n % x == 0:
    > print n, 'equals', x, '*', n/x
    > break
    > else:
    > # loop fell through without finding a factor
    > print n, 'is a prime number'

    <snip>

    A quote from "Python: Essential Reference (2/e)" (p56) says it best:

    "The 'else' clause of a loop executes only if the loop runs to completion.
    This either occurs immediately (if the loop wouldn't execute at all) or
    after the last iteration. On the other hand, if the loop is terminated
    early using the 'break' statement, the 'else' clause is skipped."

    Ross
     
    Ross Wilson, Jul 15, 2005
    #3
  4. David Smith

    David Smith Guest

    Re: Newbie question: Explain this behavior - a followup

    First, thanks to those who offered answers. They didn't really answer
    my question, only because I had not worked through the example
    sufficiently well. Doing this, I believe I understand what is
    happening, and, if my understanding is correct, have discovered that for
    other beginning and ending values for the two range statements, the
    example doesn't work.

    Given that the beginning and ending values for the inner range statement
    are the same, the inner range statement will never be executed for its
    first iternation; the else will be. This is not correct. Simply make
    the beginning value a non-prime number, and the program still prints out
    that that number is prime. Changing both beginning and ending values on
    the two statements, the ouput is differentially buggy.
     
    David Smith, Jul 15, 2005
    #4
  5. David Smith

    max Guest

    Re: Newbie question: Explain this behavior - a followup

    David Smith <> wrote in
    news:db99ec$2epm$:

    > range statements, the example doesn't work.
    >
    > Given that the beginning and ending values for the inner range
    > statement are the same, the inner range statement will never be


    Is your question about the semantics of for else blocks or about the
    suitability of the algorithm given in the example? The for else block
    is behaving exactly as expected...

    >>> range(1,1)

    []
    >>> range(500,500)

    []
    >>>


    see
    http://groups-
    beta.google.com/group/comp.lang.python/browse_frm/thread/d6c084e791a00
    2f4?q=for+else&hl=en&

    for a good explanation of when the else part of the loop is executed.
    Basically, whenever the loop is exited normally, which is what happens
    when you iterate over an empty list like the one returned by
    range(1,1)


    max
     
    max, Jul 16, 2005
    #5
  6. David Smith

    David Smith Guest

    Re: Newbie question: Explain this behavior - a followup

    max wrote:
    > David Smith <> wrote in
    > news:db99ec$2epm$:
    >
    >
    >>range statements, the example doesn't work.
    >>
    >>Given that the beginning and ending values for the inner range
    >>statement are the same, the inner range statement will never be

    >
    >
    > Is your question about the semantics of for else blocks or about the
    > suitability of the algorithm given in the example? The for else block
    > is behaving exactly as expected...
    >
    >


    Good question. The question was directed at the latter, the suitability
    of algorithm for determining prime numbers.

    >>>>range(1,1)

    >
    > []
    >
    >>>>range(500,500)

    >
    > []
    >
    >
    > see
    > http://groups-
    > beta.google.com/group/comp.lang.python/browse_frm/thread/d6c084e791a00
    > 2f4?q=for+else&hl=en&
    >
    > for a good explanation of when the else part of the loop is executed.
    > Basically, whenever the loop is exited normally, which is what happens
    > when you iterate over an empty list like the one returned by
    > range(1,1)
    >
    >
    > max
    >
    >
     
    David Smith, Jul 20, 2005
    #6
    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. Amelyan
    Replies:
    6
    Views:
    506
    Amelyan
    Jun 27, 2005
  2. =?Utf-8?B?cmdyYW5kaWRpZXI=?=

    Please explain strange viewstate behavior...

    =?Utf-8?B?cmdyYW5kaWRpZXI=?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    430
    Eliyahu Goldin
    Oct 27, 2005
  3. Replies:
    6
    Views:
    382
    Bjorn Abelli
    Jun 6, 2005
  4. Replies:
    1
    Views:
    379
    brucie
    Dec 3, 2003
  5. Christopher Richards

    Can't explain this strange behavior

    Christopher Richards, Feb 15, 2005, in forum: HTML
    Replies:
    3
    Views:
    414
    Travis Newbury
    Feb 15, 2005
Loading...

Share This Page