[Tutor] [Python-Help] help at python.org

bob gailer bgailer at alum.rpi.edu
Mon Feb 11 21:30:24 CET 2008


Artur Sousa wrote:
> What's the difference between:
>
> <code>
> for a in range(2, 10):
>      for n in range(2, a):
>          if a % n == 0:
>              print a, 'equals', n, '*', a/n
>              break
>      else:
>              print a, 'is a prime number'
> </code>
>
> and
>
> <code>
> for a in range(2, 10):
> 	for n in range(2, a):
> 		if a % n ==0:
> 			print a, 'equals', n, '*', a/n
> 			break
> 		else:
> 			print a, 'is a prime number'
> </code>
>   
Most of us monitor tutor and help - so initially pleas just post to one. 
Tutor is probably the best in this case.

The obvious difference is the placement of the else statement. In the 
first else pairs with for and is invoked when no break happens. In the 
second else pairs with if and is invoked each time the divisor test fails.

If that does not help you decide which is correct (it should) then run 
the 2 programs examine the results and that should reveal which gives 
you what you want.



-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list