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

Flynn, Stephen (L & P - IT) Steve.Flynn at capita.co.uk
Mon Dec 2 16:25:10 CET 2013


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


More information about the Tutor mailing list