[Tutor] For/Else loops

Jerry Hill malaclypse2 at gmail.com
Tue Feb 7 20:09:34 CET 2012


On Tue, Feb 7, 2012 at 1:50 PM, Debashish Saha <silideba at gmail.com> wrote:
> for i in range(1, 8):
>    print(i)
>    if i==3:
>        break
> else:
>    print('The for loop is over')
>
>
>  Output:
> 1
> 2
> 3
>
> Question:but after breaking the for loop why the else command could not work?

Because that's the way a for/else statement works.  The else suite is
only run if you do not break out of the loop.  See the docs here:
http://docs.python.org/reference/compound_stmts.html#the-for-statement

Jerry


More information about the Tutor mailing list