Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'sys': 0.07; '__name__': 0.09; 'clause': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; "'__main__':": 0.16; "'break'": 0.16; '1):': 0.16; 'indent': 0.16; 'tends': 0.16; 'true:': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.18; "python's": 0.19; 'result.': 0.19; 'thanks.': 0.20; 'input': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; '(a)': 0.24; 'instead.': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'extension': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; '(which': 0.31; 'code': 0.31; 'lines': 0.31; 'accidentally': 0.31; 'sep': 0.31; 'url:python': 0.33; 'fri,': 0.33; 'editor': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'too': 0.37; 'two': 0.37; 'level': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'skip:& 20': 0.39; 'url:mail': 0.40; 'ian': 0.60; 'number,': 0.60; 'worry': 0.60; 'break': 0.61; "you'll": 0.62; 'different': 0.65; 'relatively': 0.65; "'for'": 0.84; 'ethan': 0.84; 'furman': 0.84; 'working,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc :content-type; bh=L2y3pmk4EF2suCJwSeSd1tTju9CyHNSRPrhYF1BPT14=; b=Th4k9HiJ4sZhx6OnJvjb9yj/quYLADAWTWcIKcc2zHabo/3BwYApFYvhuNQs7FiDdH 3GglUI4VHI5umZp+hxCt2XPjG5APT2a9V3x+/+ELee3CMEDHCZtdJKxOkR6r1aGCzdDZ f1trgb3LpJK0/teN+fY5ma1zDaWiGV8fqRQHOHadC+EdFlKUX02kIw4YhrIwhxlIhBGY hMI9F1vCD4j4ZC1LRiX6255I9bAzwvCUMT7mCl0Zk6oczu7pqlVB+KS4y2mwjSe+RHEu okhMnUWeERyj+b/MvqZKZ93OYkjTtywO1InA91bav1gmYqEEvtsvY87W9SgR57rT+0vE G3MQ== X-Received: by 10.112.156.10 with SMTP id wa10mr12978503lbb.68.1409938568321; Fri, 05 Sep 2014 10:36:08 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> <5409EE02.7030308@stoneleaf.us> From: Juan Christian Date: Fri, 5 Sep 2014 14:35:48 -0300 Subject: Re: My backwards logic Cc: Python Content-Type: multipart/alternative; boundary=089e01160952851f7f050254e7c8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 109 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409938576 news.xs4all.nl 2906 [2001:888:2000:d::a6]:59827 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77601 --089e01160952851f7f050254e7c8 Content-Type: text/plain; charset=UTF-8 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): 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))) if __name__ == '__main__': prime_checker(int(sys.argv[1]), int(sys.argv[2])) On Fri, Sep 5, 2014 at 2:17 PM, Ian Kelly wrote: > On Fri, Sep 5, 2014 at 11:08 AM, Ethan Furman wrote: > > Python's 'for' loop has a handy 'else' extension which is perfect for the > > search-type of 'for' loop: > > > > while True: > > a=random.randrange(1,8) > > print (a) > > for x in range(2,a): > > if a%x==0: > > print ("Number is not prime") > > break > > else: > > print ("Number is prime") > > wait = input (" "*40 + "Wait") > > > > Note the two lines I added after the 'break' and before the 'wait'. > > Also note that this construct tends to be particularly sensitive to > indentation. If you accidentally indent the else block one level too > many (which your editor may well do for you to "helpfully" match it up > with the if), then you'll get a completely different result. > > I would not worry about the else clause as a beginner, as it's > relatively unique to Python and tends to be somewhat confusing. Use a > flag or refactor the function instead. > -- > https://mail.python.org/mailman/listinfo/python-list > --089e01160952851f7f050254e7c8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I made this code just for fun and learning, it's worki= ng, but would this be a good approach? Thanks.

impo= rt sys


def prime_checker(start =3D = 1, stop =3D 1):
for number in range(start, stop + 1):
divisors =3D [(number % x) for x in range(1, num= ber + 1)]
pri= nt("{n} prime? {r}".format(n =3D number, r =3D (divisors.count(0)= =3D=3D 2)))


if __name__ =3D=3D = 9;__main__':
prime_checker(int(sys.argv[1]), int(sys.argv[2]))


On Fri, Sep 5, 201= 4 at 2:17 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
On Fri, Sep 5, 2014 at 11= :08 AM, Ethan Furman <ethan@stonel= eaf.us> wrote:
> Python's 'for' loop has a handy 'else' extension w= hich is perfect for the
> search-type of 'for' loop:
>
>=C2=A0 =C2=A0 while True:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0a=3Drandom.randrange(1,8)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print (a)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for x in range(2,a):
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if a%x=3D=3D0:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print (&q= uot;Number is not prime")
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0break
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print ("Number is = prime")
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0wait =3D input (" "*40=C2= =A0 + "Wait")
>
> Note the two lines I added after the 'break' and before the &#= 39;wait'.

Also note that this construct tends to be particularly sensitive to<= br> indentation. If you accidentally indent the else block one level too
many (which your editor may well do for you to "helpfully" match = it up
with the if), then you'll get a completely different result.

I would not worry about the else clause as a beginner, as it's
relatively unique to Python and tends to be somewhat confusing. Use a
flag or refactor the function instead.
--
https://mail.python.org/mailman/listinfo/python-list

--089e01160952851f7f050254e7c8--