<div dir="ltr">I made this code just for fun and learning, it's working, but would this be a good approach? Thanks.<div><br></div><div><div>import sys</div><div><br></div><div><br></div><div>def prime_checker(start = 1, stop = 1):</div><div><span class="" style="white-space:pre">        </span>for number in range(start, stop + 1):</div><div><span class="" style="white-space:pre">              </span>divisors = [(number % x) for x in range(1, number + 1)]</div><div><span class="" style="white-space:pre">            </span>print("{n} prime? {r}".format(n = number, r = (divisors.count(0) == 2)))</div><div><br></div><div><br></div><div>if __name__ == '__main__':</div><div><span class="" style="white-space:pre">      </span>prime_checker(int(sys.argv[1]), int(sys.argv[2]))</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Sep 5, 2014 at 2:17 PM, Ian Kelly <span dir="ltr"><<a href="mailto:ian.g.kelly@gmail.com" target="_blank">ian.g.kelly@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Fri, Sep 5, 2014 at 11:08 AM, Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:<br>
> Python's 'for' loop has a handy 'else' extension which is perfect for the<br>
> search-type of 'for' loop:<br>
><br>
>    while True:<br>
>         a=random.randrange(1,8)<br>
>         print (a)<br>
>         for x in range(2,a):<br>
>             if a%x==0:<br>
>                 print ("Number is not prime")<br>
>                 break<br>
>         else:<br>
>             print ("Number is prime")<br>
>         wait = input (" "*40  + "Wait")<br>
><br>
> Note the two lines I added after the 'break' and before the 'wait'.<br>
<br>
</span>Also note that this construct tends to be particularly sensitive to<br>
indentation. If you accidentally indent the else block one level too<br>
many (which your editor may well do for you to "helpfully" match it up<br>
with the if), then you'll get a completely different result.<br>
<br>
I would not worry about the else clause as a beginner, as it's<br>
relatively unique to Python and tends to be somewhat confusing. Use a<br>
flag or refactor the function instead.<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="https://mail.python.org/mailman/listinfo/python-list" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>