[Tutor] Prime Numbers

Alan Gauld alan.gauld at btinternet.com
Sun Dec 15 19:00:49 CET 2013


On 15/12/13 16:54, Rafael Knuth wrote:

> I stumbled upon this program here (Python 3.3.0) and I don't quite
> understand how the for loop plays with the return True statement:
>

It doesn't.
Remember that indentation is all important in Python.
The return true statement is outside the loop so only
gets executed if all the numbers in the loop range
have been tested.


> def is_prime(number):
>      for element in range(2, number):
>          if number % element == 0:
>              return False
>      return True

You seem to be confusing it with an if/else construct,
but this is not an else condition of the if.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list