Python 15.6 Is_Prime


Hello Friends,I am once again seeking your help because I am stuck at Following is my code

def is_prime(x):
    if x<=1:
        return False
    elif x==2 or x==3:
        return True
        for n in range (2,x-1):
            if x%n==0:
                return False
                return True
print is_prime(1)
print is_prime(2)
print is_prime(3)
print is_prime(4)

Also,Does any body thinks my "elif": statement is cheating.


Are 2 and 3 really special cases?
In each iteration of your loop, you either exit the function, or you exit the function. In other words, there will never be a second iteration.


Please, can you tell me how to fix it ?


Refer to how you do it manually. If you don't understand what the code is doing then you can read more about whatever you don't understand what it does, and you can also use print statements to see what order it does things in


I solved the problem .Just "untab" the final else statement only once


i think this code is more correct:

def is_prime(x):
    count = 0
    for i in range(1, x+1):
        if x % i != 0:
            count += 1
    if count == 2:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.