Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77605
| References | <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> <mailman.13795.1409936907.18130.python-list@python.org> <a1tj0a9sb7bo0qd2lpf7gl9gu3l0o1sniu@4ax.com> |
|---|---|
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | 2014-09-05 11:17 -0700 |
| Subject | Re: My backwards logic |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13802.1409941356.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Fri, Sep 5, 2014 at 10:44 AM, Seymore4Head <Seymore4Head@hotmail.invalid>
wrote:
> On Fri, 05 Sep 2014 10:08:18 -0700, Ethan Furman <ethan@stoneleaf.us>
> wrote:
>
> >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'.
>
> I had already tried this one.
> The solution I want should only print:
> "This number is prime"
>
> Adding else causes the program to also print "This number is not
> prime"
>
If you do not want it to print the "Number is not prime", just remove the
print("Number is not prime") line.
> I also tried the flag=True suggestion, but never got one that worked.
> I am unsure when to use flag=True and flag==True
> Then there is flag="True" and flag=="True"
>
Generally, you would want to use:
flag = False
before the loop, and
flag = True
inside the loop (once you know the number is not prime). After the loop,
you would typically just use:
if flag: # This could be "if flag == True:", however the plain "if flag:"
is more Pythonic.
print("Number is prime")
The "=" operator is assignment - storing a new value.
The "==" operator is for equality checking. The inverse is the "!="
operator which checks for inequality.
What I really wanted was something like:
> if !(a%x==0)
In this case, that will not work, as you only know that the number is prime
after checking all the values, which means after the loop has completed.
Ignoring the fact that it would not function for this use case, the proper
Python syntax for that would be one of:
- if a%x != 0: # Probably the clearest for this case.
- if not (a%x==0):
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