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
    <http://docs.python.org/3/reference/simple_stmts.html#break> and
    continue
    <http://docs.python.org/3/reference/simple_stmts.html#continue>
    Statements, and else
    <http://docs.python.org/3/reference/compound_stmts.html#else>
    Clauses on Loops

The break <http://docs.python.org/3/reference/simple_stmts.html#break> statement, like in C, breaks out of the smallest enclosing for <http://docs.python.org/3/reference/compound_stmts.html#for> or while <http://docs.python.org/3/reference/compound_stmts.html#while> loop.

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for <http://docs.python.org/3/reference/compound_stmts.html#for>) or when the condition becomes false (with while <http://docs.python.org/3/reference/compound_stmts.html#while>), but not when the loop is terminated by a break <http://docs.python.org/3/reference/simple_stmts.html#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  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to