Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'else:': 0.03; 'syntax': 0.04; 'explicit': 0.07; 'clause': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'wrote': 0.14; 'itself.': 0.14; 'books': 0.15; 'count,': 0.16; 'excludes': 0.16; 'executed.': 0.16; 'itself).': 0.16; 'received:80.91.229.3': 0.16; 'received:97': 0.16; 'received:plane.gmane.org': 0.16; 'requested.': 0.16; 'true:': 0.16; 'all.': 0.16; '(not': 0.18; 'library': 0.18; 'any,': 0.19; 'not,': 0.20; '(the': 0.22; 'input': 0.22; 'print': 0.22; '(a)': 0.24; 'test.': 0.24; "haven't": 0.24; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; 'code': 0.31; 'default,': 0.31; 'factor': 0.31; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'doing': 0.36; 'url:org': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'heard': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'problems.': 0.60; 'then,': 0.60; 'break': 0.61; 'telling': 0.64; 'prime': 0.74; 'divide': 0.84; 'flag.': 0.84; 'url:2014': 0.84; 'subject::': 0.85; 'url:16': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re:My backwards logic Date: Fri, 5 Sep 2014 13:15:23 -0400 (EDT) Organization: news.gmane.org References: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 97.73.240.72 X-Newsreader: PiaoHong.Usenet.Client.VIP:1.59 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409937228 news.xs4all.nl 2880 [2001:888:2000:d::a6]:45601 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77597 Seymore4Head Wrote in message: > I'm still doing practice problems. I haven't heard from the library > on any of the books I have requested. > > http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html > > This is not a hard problem, but it got me to thinking a little. A > prime number will divide by one and itself. When setting up this > loop, if I start at 2 instead of 1, that automatically excludes one of > the factors. Then, by default, Python goes "to" the chosen count and > not "through" the count, so just the syntax causes Python to rule out > the other factor (the number itself). > > So this works: > 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 > wait = input (" "*40 + "Wait") > > But, what this instructions want printed is "This is a prime number" > So how to I use this code logic NOT print (not prime) and have the > logic print "This number is prime" > > The traditional way of telling whether something happened in a loop is to set a flag to False outside the loop, and conditionally set it to True in the if test. Then after the loop, check your flag. Python however has a better way. You can put an else clause on the for loop: for x in range(2,a): if a%x==0: print ("Number is not prime") break else: print ("Number is prime") The else clause fires if no break executed. There are also ways to do it using not, any, and a list comprehension, no explicit loop at all. -- DaveA