Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77596
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ckaynor@zindagigames.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; '"this': 0.03; 'else:': 0.03; 'syntax': 0.04; 'result,': 0.07; 'function,': 0.09; 'logic': 0.09; 'python': 0.11; 'def': 0.12; 'itself.': 0.14; 'books': 0.15; 'count,': 0.16; 'excludes': 0.16; 'itself).': 0.16; 'python),': 0.16; 'requested.': 0.16; 'true:': 0.16; 'wrote:': 0.18; '(not': 0.18; 'library': 0.18; '(the': 0.22; 'input': 0.22; 'to:name :python-list@python.org': 0.22; 'print': 0.22; '(a)': 0.24; "haven't": 0.24; 'header:In-Reply-To:1': 0.27; 'feature': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'default,': 0.31; 'extract': 0.31; 'factor': 0.31; 'sep': 0.31; 'run': 0.32; 'another': 0.32; 'fri,': 0.33; 'received:74.125.82': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'doing': 0.36; 'url:org': 0.36; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'received:74.125': 0.39; 'heard': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'problems.': 0.60; 'then,': 0.60; 'break': 0.61; 'new': 0.61; 'advanced': 0.63; 'relatively': 0.65; 'prime': 0.74; 'completion': 0.78; 'code):': 0.84; 'divide': 0.84; 'received:74.125.82.172': 0.84; 'url:2014': 0.84; 'url:16': 0.93 |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=cnmNwb/h0TMeV2jpnagL384dflLNeVZSyLdQ6l9Pub4=; b=I4qksZpO0vHMPxsir7THOYRLZtfWf98jRI3ovKmwSCKqPVhYuUC0MTgSwI8iZU52UR tJI42Np+Sfgpxm3bk2LYxgjA7n2N7TDj5+7A/75tKhvvCTe9wW0tTFWuhdkANyBT1Oh3 j4RmXIB2RcyCl+bei3mdcn4W3dgKDvuaU2yIAqLPox/L+G8BqaRxyND5IIryZ/8C6Gc4 YTKFOsIAO1PpXUAcb2NrC0DrFIHps1kYNNq7MPTSt4jQmlZAGOu6fI2Bo+ZKIW78mOrN j6Z71NXNXKn1u4OSfEMx1l+mr08YaYQ/XVQkauB/p9rqoWAi/UiRdonCxKznUnD/SLA4 aWeQ== |
| X-Gm-Message-State | ALoCoQnjCZ9rey5kPeoTsoeuoeVqfuyINSX+7lP2d08y/mUrB4hfkuV9P47AwNTH9c2lwtBttW2x |
| X-Received | by 10.194.202.231 with SMTP id kl7mr4865523wjc.134.1409936965476; Fri, 05 Sep 2014 10:09:25 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> |
| References | <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> |
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | Fri, 5 Sep 2014 10:09:05 -0700 |
| Subject | Re: My backwards logic |
| To | "python-list@python.org" <python-list@python.org> |
| Content-Type | multipart/alternative; boundary=047d7bae4836fbb7ce05025487f2 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13796.1409936972.18130.python-list@python.org> (permalink) |
| Lines | 139 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1409936972 news.xs4all.nl 2890 [2001:888:2000:d::a6]:38801 |
| X-Complaints-To | abuse@xs4all.nl |
| X-Received-Bytes | 9693 |
| X-Received-Body-CRC | 2556174981 |
| Xref | csiph.com comp.lang.python:77596 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Fri, Sep 5, 2014 at 9:48 AM, Seymore4Head <Seymore4Head@hotmail.invalid>
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"
>
>
One neat feature of Python is the for...else function. The code inside the
else block will run only if the loop ran to completion (untested code
follows):
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")
Depending on how advanced you want to get (I know you are relatively new to
Python), another decent way would be to extract the prime check to a
function, and return the result, and print based on that result (again,
untested code):
def isPrime(number):
for x in range(2,a):
if a%x==0:
return False
return True
while True:
a=random.randrange(1,8)
print (a)
if isPrime(a):
print("Number is prime")
else:
print("Number is not prime")
wait = input (" "*40 + "Wait")
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 12:48 -0400
Re: My backwards logic MRAB <python@mrabarnett.plus.com> - 2014-09-05 17:57 +0100
Re: My backwards logic Bob Gailer <bgailer@gmail.com> - 2014-09-05 13:04 -0400
Re: My backwards logic John Gordon <gordon@panix.com> - 2014-09-05 17:08 +0000
Re: My backwards logic Ethan Furman <ethan@stoneleaf.us> - 2014-09-05 10:08 -0700
Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 13:44 -0400
Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 11:17 -0700
Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 15:04 -0600
Re: My backwards logic Chris Angelico <rosuav@gmail.com> - 2014-09-06 09:44 +1000
Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 10:09 -0700
Re:My backwards logic Dave Angel <davea@davea.name> - 2014-09-05 13:15 -0400
Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 11:17 -0600
Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 14:35 -0300
Re: My backwards logic Ethan Furman <ethan@stoneleaf.us> - 2014-09-05 11:41 -0700
Re: My backwards logic MRAB <python@mrabarnett.plus.com> - 2014-09-05 19:48 +0100
Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 16:34 -0300
Re: My backwards logic Paul Rubin <no.email@nospam.invalid> - 2014-09-05 13:07 -0700
Re: My backwards logic Rustom Mody <rustompmody@gmail.com> - 2014-09-05 18:24 -0700
Re: My backwards logic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-05 20:54 +0100
Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 17:49 -0400
Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 15:14 -0700
Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 18:36 -0400
Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 16:35 -0600
Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 19:05 -0400
Re: My backwards logic Dave Angel <davea@davea.name> - 2014-09-05 23:10 -0400
Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 22:54 -0300
Re: My backwards logic Rustom Mody <rustompmody@gmail.com> - 2014-09-05 18:58 -0700
Posting style: interleaved responses (was: My backwards logic) Ben Finney <ben+python@benfinney.id.au> - 2014-09-06 12:37 +1000
Re: Posting style: interleaved responses (was: My backwards logic) Juan Christian <juan0christian@gmail.com> - 2014-09-06 00:06 -0300
Re: Posting style: interleaved responses (was: My backwards logic) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-09-05 23:04 -0500
Re: My backwards logic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-06 08:45 +0100
Re: My backwards logic Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-06 07:49 +0000
Prime testing [was Re: My backwards logic] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-06 20:38 +1000
Re: Prime testing [was Re: My backwards logic] Chris Angelico <rosuav@gmail.com> - 2014-09-06 20:42 +1000
Re: Prime testing [was Re: My backwards logic] Manolo Martínez <manolo@austrohungaro.com> - 2014-09-06 12:53 +0200
Re: Prime testing [was Re: My backwards logic] Peter Pearson <pkpearson@nowhere.invalid> - 2014-09-07 18:53 +0000
Re: Prime testing [was Re: My backwards logic] Manolo Martínez <manolo@austrohungaro.com> - 2014-09-07 21:09 +0200
Re: Prime testing [was Re: My backwards logic] Dan Stromberg <drsalists@gmail.com> - 2014-09-07 20:23 -0700
Re: Posting style: interleaved responses Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-06 08:46 +0100
csiph-web