# 6.is_prime,have no idea what i wrote

#1
def is_prime(x):
if x >1:
if x ==2:
return True
else:
for n in range(2,x):
if x % n == 0 :
return False
break
n += 1
else:
return True
else:
return False

I have no idea what i wrote,can anyone explain to me ,
like,whether the " break " is necessary?
why is the 2nd else indented there, not elsewhere? i mean in my 1st attempt, it's inside "for".
why is n+1 outer than break?
why is it "if" not "while"?
FYI,I read some posts,they write more codes to filter the cases that the number is even,or cut the original x into square, i am too lazy,despite i know ,(well,maybe I don't know) it might help the programme to run faster.

#2

Are you the sole author of this code? Because the way you are asking the questions make it seem like you have no idea what is going on

#3

yes i am , but i referred to others' responses as well.
i am a lot confused about the differences between if and while.
lets just say ,i tried many times, and now it seems to work, but i don't know why its working,not very precisely.
excuse my english, i am chinese

#4

no in this for loop the break and this line

are not necessary because you have set it to run only for this finite interval range(2, x(the number you pass)) and will stop x-1
the for loop will implicitly increment no need to do that

The break statement is unreachable(can never be executed) and it is not even needed because when you call return in a method it terminates the method

#5
def is_prime1(x):
n=x//2
while n>1:
if x % n == 0 :
print (x,'has factor',n)
return False
break
n -= 1
else:
print (x,'is prime')
return True

I think i am getting to understand it now, i seem to get the 'while' and 'else' now.
but still don't get the ' else' and 'for'

#6

for and else are the same as while and else. You are looking at something to execute at the end of the loop if the loop ends naturally (without a break). So when you get to the end of your for loop, else will execute unless you have a break somewhere in there.

#7

this line is not needed