问题
I am learning python and was looking at this problem: Python - Prime Number exercise Here are my questions: When n=2, the range will be (2,n), in the other words the range is between 2 and 2-1=1.
for x in range(2, n):
if n % x == 0:
print("{} equals {} x {}".format(n, x, n // x))
return False
else:
print(n, "is a prime number")
return True
a. Will it be 2%2?
b. If the number is even, it will print
A equals B x C
will the loop break once the condition is true or it will finish the loop?
c. is the else outside the loop? what if statement does it correspond? (i know it is the if n % x == 0:
) but howcome it is seems like it is outside the loop?
I tried to run it, and understand, but then I got confused.
回答1:
for .. else confusion
You seem to be confused by else
which is part of the for
loop.
You are not the only one.
In Python, for
loop might have at the end else
section, which is executed after all the loops are finished, unless there was break
statement. Yes, it is not very intuitive.
So you shall read the else
section as last part of the for
loop.
def looptest(loops=5):
for i in range(loops):
print "i", i
if i == 3:
print "in if"
return
else:
print "in else of the loop"
and try it for loops == 5:
>>> looptest(5)
i 0
i 1
i 2
i 3
in if
For number of loops 2 we do not expect return, we shall run through else
of the loop:
>>> looptest(2)
i 0
i 1
in else of the loop
Note, that return
must be within function, trying return on console would result in error.
range(2, 2) == []
Trying on Python console:
>>> range(2, 2)
[]
return
return ends up current function or run in the whole module. So after return
, no further code in the same part of your code is executed (with exceptions like try
, finally
, context managers etc, but this is not relevant to your code).
回答2:
Take a look at this function:
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i ==0:
return False
return True
Iterating through every possible option isn't necessarily the best option; keep in mind the range will never be more than the square root of the number.
Also worth noting, your output will only print the first set of factors.
回答3:
a. No. range(2,2)
is an empty set in Python.
b. As soon as the condition is true, the function that contains the loop will return, and the loop will not finish. That's because the return
call is one level down from the if
statement.
c. I'm pretty sure you want the else
to correspond with the for
. Python's got this for...else syntax that executes the content of the else
clause if the for
loop terminates normally (i.e. without a break
).
In other words, I think you want this:
for x in range(2, n):
if n % x == 0:
print("{} equals {} x {}".format(n, x, n // x))
return False
else:
print(n, "is a prime number")
return True
EDIT:
Sure, I can explain the whole loop. The idea is this: for each number between two and n, check if that number is a factor of x. If it is, then n isn't prime, and we kill the loop. However, if we make it all the way to the end of the loop, we know that n is prime, because it has no factors between 2 and n. The else clause then kicks in, and prints that the number is prime.
来源:https://stackoverflow.com/questions/23395201/python-prime-numbers-and-the-in-range