Sean Russell schrieb:
> I feel like such a novice... but would someone explain to me why such a
> construction would be necessary? Specifically, for ... else ... end.  What
> is the difference between:

[...Your example...]

(No difference here, as no break is used.)


Extract from the Python doc:

<cite>
Loop statements may have an else clause; it is 
executed when the loop terminates through 
exhaustion of the list (with for) or when the 
condition becomes false (with while), but not when 
the loop is terminated by a break statement. 
This is exemplified by the following loop, which 
searches for prime numbers: 


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

</cite>

(REM: the range() function creates a list of items
*excluding* the end point. I.e.: n itself is *not* part
of the range ).

The situation that you want to know the exact
status after a loop is very common IMHO, and
is in classic languages (C, C++, Java, ...) 
typically solved by using flag variables like this:

for n in 2..9
    myflag = 0
    for x in 2..(n-1)  # see remark above
      var = n % x
       if var == 0
          print n, ' equals ', x, " * ", n/x, "\n"
          myflag = 1
          break
       end
    end
    if myflag == 0
       # loop fell through without finding a factor
       print n, " is a prime number\n"
    end
end

HTH

Det