Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'else:': 0.03; 'syntax': 0.04; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'logic': 0.09; 'message-id:@stoneleaf.us': 0.09; '~ethan~': 0.09; 'python': 0.11; 'itself.': 0.14; 'books': 0.15; "'break'": 0.16; 'count,': 0.16; 'excludes': 0.16; 'itself).': 0.16; 'requested.': 0.16; 'true:': 0.16; 'wrote:': 0.18; '(not': 0.18; 'library': 0.18; "python's": 0.19; '(the': 0.22; 'input': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '(a)': 0.24; "haven't": 0.24; 'extension': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'default,': 0.31; 'factor': 0.31; 'but': 0.35; 'doing': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'heard': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'problems.': 0.60; 'then,': 0.60; 'break': 0.61; 'received:173': 0.61; 'prime': 0.74; "'for'": 0.84; 'divide': 0.84; 'url:2014': 0.84; 'url:16': 0.93 Date: Fri, 05 Sep 2014 10:08:18 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: My backwards logic References: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> In-Reply-To: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409936907 news.xs4all.nl 2895 [2001:888:2000:d::a6]:37544 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77595 On 09/05/2014 09:48 AM, Seymore4Head wrote: > 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" 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'. -- ~Ethan~