Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie question: Explain this behavior

Reply
Thread Tools

Newbie question: Explain this behavior

 
 
David Smith
Guest
Posts: n/a
 
      07-14-2005
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

 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      07-15-2005
"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.htm...00000000000000) uses the prime number
computation you posted, so read it again if it's not clear what the "else" clause does with loops.

George


 
Reply With Quote
 
 
 
 
Ross Wilson
Guest
Posts: n/a
 
      07-15-2005
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
 
Reply With Quote
 
David Smith
Guest
Posts: n/a
 
      07-15-2005
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.


 
Reply With Quote
 
max
Guest
Posts: n/a
 
      07-16-2005
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


 
Reply With Quote
 
David Smith
Guest
Posts: n/a
 
      07-20-2005
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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain strange viewstate behavior... =?Utf-8?B?cmdyYW5kaWRpZXI=?= ASP .Net 1 10-27-2005 08:57 AM
Requires help of true Super-MVP to explain a particular aspect of Asp.Net behavior? Amelyan ASP .Net 6 06-27-2005 02:31 PM
Explain odd scope behavior? kk_oop@yahoo.com Java 6 06-06-2005 04:50 PM
Can't explain this strange behavior Christopher Richards HTML 3 02-15-2005 06:30 PM
Can someone explain this IE/Opera behavior to me? delerious@no.spam.com HTML 1 12-03-2003 04:35 AM



Advertisments