My backwards logic
ethan at stoneleaf.us
Fri Sep 5 19:08:18 CEST 2014
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.
> 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:
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> 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:
for x in range(2,a):
print ("Number is not prime")
print ("Number is prime")
wait = input (" "*40 + "Wait")
Note the two lines I added after the 'break' and before the 'wait'.
More information about the Python-list