My backwards logic

MRAB python at mrabarnett.plus.com
Fri Sep 5 20:48:34 CEST 2014


On 2014-09-05 18:35, Juan Christian wrote:
> I made this code just for fun and learning, it's working, but would this
> be a good approach? Thanks.
>
> import sys
>
>
> def prime_checker(start = 1, stop = 1):

In Python, the standard is to use a half-open range.

>     for number in range(start, stop + 1):
>         divisors = [(number % x) for x in range(1, number + 1)]
>             print("{n} prime? {r}".format(n = number, r = (divisors.count(0) == 2)))
>
You want to know only whether it's prime, so why continue looking after
finding a factor?
>
> if __name__ == '__main__':
>     prime_checker(int(sys.argv[1]), int(sys.argv[2]))
>
[snip]




More information about the Python-list mailing list