Is_prime


#1

Can t make this work, It keeps saying .. Oops, try again. Your function fails on is_prime(2). It returns False when it should return True.

x = input("Insert Number: ")

def is_prime(x):
if x == 1 and x == 2 and x == 3:

    return False


elif x >2:
    for n in range(2,x-1):


        if  x%n == 0:

            return False
            
        else:

            return True

else:
    return False

print is_prime(x)


#2

Does this seem odd? 2 and 3 are both Primes.

The if statement should not have an else branch.

This final else statement is going to be a problem.


#5

It seems everything I copied got messed up when I pasted it

Here 's again ..

x = input("Insert Number: ")

def is_prime(x):
    if x == 1 and x == 2 and x == 3:
        
        return False
    
    
    elif x >2:
        for n in range(2,x-1):
            

            if  x%n == 0:

                return False
                
            
            else:
    
                return True
    
    else:
        return False



print is_prime(x)

#9

Paste in your code, then select it all, then click the </> button to format it. This will enclose the code between two sets of three back-ticks.

```python

code sample

```

Scroll back to the top of the code sample and add in the language name as shown above. No white space, and all lowercase.


#10

The only thing we need to check is if x is less than 2

    if x < 2: return False
    for n in range(2, x - 1):
        # test for divisibility and return False
    return True

The if statement inside the for loop should not have an else branch.


#11

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