Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'else:': 0.03; 'operator': 0.03; 'syntax': 0.04; 'assignment': 0.07; '"if': 0.09; 'logic': 0.09; 'worked.': 0.09; 'python': 0.11; 'itself.': 0.14; 'books': 0.15; '"="': 0.16; '"=="': 0.16; '>>': 0.16; "'break'": 0.16; '>on': 0.16; 'count,': 0.16; 'excludes': 0.16; 'ignoring': 0.16; 'inverse': 0.16; 'itself).': 0.16; 'requested.': 0.16; 'storing': 0.16; 'true:': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.18; '(not': 0.18; 'library': 0.18; 'value.': 0.19; 'work,': 0.20; '(the': 0.22; 'input': 0.22; 'to:name:python- list@python.org': 0.22; 'print': 0.22; '(a)': 0.24; 'case.': 0.24; "haven't": 0.24; '>': 0.26; 'extension': 0.26; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; '-0700,': 0.31; 'checking.': 0.31; 'default,': 0.31; 'equality': 0.31; 'factor': 0.31; 'sep': 0.31; 'probably': 0.32; 'checking': 0.33; 'fri,': 0.33; 'plain': 0.33; 'received:74.125.82': 0.34; 'could': 0.34; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'skip:& 10': 0.38; 'checks': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'received:74.125': 0.39; 'heard': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'remove': 0.60; 'problems.': 0.60; 'then,': 0.60; 'break': 0.61; 'new': 0.61; 'more': 0.64; 'of:': 0.68; 'prime': 0.74; "'for'": 0.84; 'divide': 0.84; 'ethan': 0.84; 'furman': 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=xabDmhGHwHz4/hSC7bkOCeF9KQ2z6Fb8ZzTpeBI4qn4=; b=YohU/S0yL9nsTBKy0VsSNDt9jwZbltmb7Y0Naj1uCz7KbeXvaZYNIfy04aJJ1F6rpW lKNuXrz7Ka5C/pGpz1n6a6lg2Y2cdFL/iu0g7YNTrsfOrcBQarrofeh4umkwFZ2hsf9u QUbae7K+1RjNFqlIGSDPiecjipYpNFHM0WYyYB0gVULmjBBkisVfbnrB3FRzIG3SfTNg YZDeKpRe4McPb+PWBOQx4zeFuJFDP9U+R2QYhF/l5/NtU89jJi6+n5Wei5OQxg4zzmsy F4mCCFboK5ud02lDOc21EQgwBbvgj98DWcGcf7RSRxEIUnlWtVWP3jVjTIDmd+jjNMVU 6fgg== X-Gm-Message-State: ALoCoQmj5NsntMF1KIYfIGqV0bNRmcDOHjp5xmotujeCGSGK44ET9dxn/1o5u8oR/4iTQj1LKEm+ X-Received: by 10.180.211.233 with SMTP id nf9mr5400746wic.33.1409941046231; Fri, 05 Sep 2014 11:17:26 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> From: Chris Kaynor Date: Fri, 5 Sep 2014 11:17:05 -0700 Subject: Re: My backwards logic To: "python-list@python.org" Content-Type: multipart/alternative; boundary=001a11c338d0371ad80502557bc1 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: 223 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409941356 news.xs4all.nl 2873 [2001:888:2000:d::a6]:49466 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77605 --001a11c338d0371ad80502557bc1 Content-Type: text/plain; charset=UTF-8 On Fri, Sep 5, 2014 at 10:44 AM, Seymore4Head wrote: > On Fri, 05 Sep 2014 10:08:18 -0700, Ethan Furman > 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): --001a11c338d0371ad80502557bc1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On F= ri, 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 problem= s.=C2=A0 I haven't heard from the library
>> on any of the books I have requested.
>>
>> http://www.practicepython.o= rg/exercise/2014/04/16/11-check-primality-functions.html
>>
>> This is not a hard problem, but it got me to thinking a little.=C2= =A0 A
>> prime number will divide by one and itself.=C2=A0 When setting up = this
>> loop, if I start at 2 instead of 1, that automatically excludes on= e of
>> the factors.=C2=A0 Then, by default, Python goes "to" th= e chosen count and
>> not "through" the count, so just the syntax causes Pytho= n to rule out
>> the other factor (the number itself).
>>
>> So this works:
>> while True:
>>=C2=A0 =C2=A0 =C2=A0 a=3Drandom.randrange(1,8)
>>=C2=A0 =C2=A0 =C2=A0 print (a)
>>=C2=A0 =C2=A0 =C2=A0 for x in range(2,a):
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if a%x=3D=3D0:
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print ("Numbe= r is not prime")
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break
>>=C2=A0 =C2=A0 =C2=A0 wait =3D input (" "*40=C2=A0 + "= ;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 hand= y 'else' extension which 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 inpu= t (" "*40=C2=A0 + "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 pr= int the "Number is not prime", just remove the print("Number= is not prime") line.
=C2=A0
I also tried the flag=3DTrue suggestion, but never got one that worked.
I am unsure when to use flag=3DTrue and flag=3D=3DTrue
Then there is flag=3D"True" and flag=3D=3D"True"

Generally, you would want to use:
=
flag =3D False<= /div>
=C2=A0before the loop, and
flag =3D True
inside the loop (once you know the number is not prime). After the lo= op, you would typically just use:
if flag: # This could be "if fla= g =3D=3D True:", however the plain "if flag:" is more Python= ic.
print("Number is prime")

The "=3D" operator i= s assignment - storing a new value.
The "=3D=3D" operat= or is for equality checking. The inverse is the "!=3D" operator w= hich checks for inequality.

What I really wanted was something like:
if !(a%x=3D=3D0)

In this case, that will no= t work, as you only know that the number is prime after checking all the va= lues, which means after the loop has completed.=C2=A0

=
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 !=3D 0: # Probably the clearest for this case.
  • if not (a%x=3D=3D= 0):

--001a11c338d0371ad80502557bc1--