returning none when it should be returning a list?

Roy Smith roy at panix.com
Mon May 1 08:24:12 EDT 2006


randomtalk at gmail.com wrote:

Several people have already given you good answers as to why you're getting 
None, and how to improve the algorithm you're using.  I want to address 
some stylistic questions.

First, is the

>     if(next >= lim):
>         print(seed)
>         return seed
>     else:
>         count = 0;
>         [...]

construct.  You don't need the else part, since the if clause ends with a 
return.  You can just un-indent one stop and put everything that is now 
inside the "else" at the same level as the "if".  This makes your program 
easier to read and understand.  Your program isn't too bad because it's 
only got about a dozen lines of code in the "else", and you only end up 
about 4 indents deep.  In larger programs, you can end up with 100's of 
lines of code and 5, 6, or more indents.  Then it's a nightmare to 
understand.

The other sylistic issue is this:

>         if(count == len(seed)):
>             seed.append(next)
>             findPrime(seed, next+2, lim)
>         else:
>             findPrime(seed, next+2, lim)

You've repeated the call to findPrime().  You refactor that out like:

         if(count == len(seed)):
             seed.append(next)
         findPrime(seed, next+2, lim)

Three lines of code instead of five, and no repeated code.  Easier to 
understand.



More information about the Python-list mailing list