Re: [Tutor] /tutorial/controlflow.html "break statement"

2013-12-02 Thread Flynn, Stephen (L & P - IT)
> The script studied was :
> 
> for n in range(2, 10):
>  for x in range(2, n):
>  if n % x == 0:
>  print(n, 'equals', x, '*', n//x)
>  break
>  else:
>  print(n, 'is a prime number')

The code above is not what you ran below, in idle. Look at the
indentation of the else: line, which completely alters the execution
flow of the code. It should be aligned with a the inner "for" and you
have it aligned with the inner "if".

> Here is the result given in IDLE3 on my Presario CQ61:
> 
>  >>> for n in range(2,10):
>  for x in range(2, n):
>  if n % x == 0:
>  print(n, '=',x,'*',n//x)
>  break
>  else:
>  # loop fell through without finding a factor
>  print(n, 'is a prime number')


Make the change and you'll get the output you were expecting.




This email and any attachment to it are confidential.  Unless you are the 
intended recipient, you may not use, copy or disclose either the message or any 
information contained in the message. If you are not the intended recipient, 
you should delete this email and notify the sender immediately.

Any views or opinions expressed in this email are those of the sender only, 
unless otherwise stated.  All copyright in any Capita material in this email is 
reserved.

All emails, incoming and outgoing, may be recorded by Capita and monitored for 
legitimate business purposes. 

Capita exclude all liability for any loss or damage arising or resulting from 
the receipt, use or transmission of this email to the fullest extent permitted 
by law.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] /tutorial/controlflow.html "break statement"

2013-12-02 Thread Pierre-Michel Averseng

Hello,

what do you think about the results given by IDLE3 with a script studied 
recently in T. Digest Vol 117, issue 70 & seq. ?


I'm working with Linux (Debian family => i.e Linux Mint LMDE :
[please, could you excuse my poor English... ?  Thanks !  ;^))   ]

Linux hojulien 3.10-2-486 #1 Debian 3.10.5-1 (2013-08-07) i686 GNU/Linux
Python 3.3.2+ (default, Aug  4 2013, 17:23:22)
[GCC 4.8.1] on linux

The script studied was :

for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
print(n, 'is a prime number')

Here is the result given in IDLE3 on my Presario CQ61:

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


3 is a prime number
4 = 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 = 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 = 2 * 4
9 is a prime number
9 = 3 * 3

I found this script at : 
http://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops




4.4. break
 and
continue

Statements, and else

Clauses on Loops

The break  
statement, like in C, breaks out of the smallest enclosing for 
 or while 
 loop.


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')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a

Surprising !  isn't it ?

Best regards

Pierre
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor