[Tutor] Advice for my function, isPrime(n), please

Dick Moores rdm at rcblue.com
Tue Jul 22 01:02:14 CEST 2008


At 01:42 PM 7/21/2008, Terry Carroll wrote:
>On Mon, 21 Jul 2008, Daniel Sarmiento wrote:
>
> > What about the following function?
> >
> > if x == 0:
> >     return False
> > return True
>
>I don't like it, myself.  You have multiple points of exit, and, yes, you
>can see that the fallthough is only executed if the condition is not met,
>but it makes you do a double-take when reading it.
>
>If you insist on having the conditional, this is clearer:
>
>if x == 0:
>     return False
>else:
>     return True
>
>I'd rather have the condition test for truth, though:
>
>if x != 0:
>     return True
>else:
>     return False
>
>But that leads to what you don't like anyway, which I think is your best
>solution:
>
>return x != 0

Actually, and as I predicted, I'm beginning to like it. :-)

Dick




More information about the Tutor mailing list