There are 4 things wrong with this code. Any help will be gratefully appreciated.
Firstly it will not let me enter my own number.

Secondly it tells me that 4 is a prime number
Thirdly I get an error message..."Oops, try again. Your function fails on is_prime(2). It returns None when it should return True." . In line 11 I made a provision for 2 as a prime number
Fourthly In my last print statement I am trying to say if false print not a prime number and if true print it is a prime number.
in addition, I have tried removing some of the return statements; but, no joy unfortunately

def is_prime(x):
    print  "you wish to find out if",x,"is a prime number!"
    if x ==0: 
            print "Your number is not a prime number"
            return False
    elif x==1:
        print "your number is a not a prime number"
        return False
    for n in range(2,int((x-1)**0.5)):
        if n==2: # if the number entered is 2 then print it is a prime number
            print "your number is a prime number"
            return True
        elif n>2 and n%n!=0:#if the remainder after dividing by a number between 2 and the square root of the number is has  not got 0 as a remainder, then the number is a prime number        
            print "your number is  a prime number"
            print "the number is not a prime number"
            return False
        number =  input(" enter the number to check if it is a prime number" )
print is_prime(number), number,"is a prime number"

you wish to find out if 4 is a prime number!
None 4 is a prime number
None .


I'm puzzled that you use the codes such as
if n == 2
elif n < 2

The function Is_prime is to make true that the input x is a prime or not , not the n, n is just in the for function

Maybe you could fix this and see if the errors still exist


Thank you.I shall change the code as you suggested.


To answer your first question, your second to last line of code should not be indented at all:

number = input(" enter the number to check if it is a prime number" )

To address your second and third: I played around with a similar for loop,a nd could not get the correct answer. I think that it is because the while executing the 'for' loop, one of the steps will return True. n starts off equal to 2, is checked if ==2, returns True. To fix this, I got rid of my 'for' loop and replaced it with a 'while' loop. Inside of my 'while' loop I had an if/else to check if the remainder was equal to 0, similar to what you did, to either break the 'while' loop or advance it. Hopefully that is enough detail to get you going down the right path, but not too much that I gave away the answer.

Additionally, keep an eye out for the word "None" being returned, as it is in your example, and other examples when I tried to test your code. The only numbers that do not result in a None return are 0 and 1, which means that your code is not actually responding to any inputs other than 0 and 1 because they do not fit any of the conditions.

Lastly, your final print statement :

print is_prime(number), number,"is a prime number"

has no logical test, so it will always tell you that every input is a prime number. Furthermore, look at what comes out when you input 1:

enter the number to check if it is a prime number 1
you wish to find out if 1 is a prime number!
your number is a not a prime number
False 1 is a prime number


Thank you. for the time and effort you spent on this. I am very grateful.I shall try all your recommendations.Great,thank you


I have done it now and it works. Thank you


Can someone help me understand the reasoning for this line:

for n in range(2,int((x-1)**0.5)):

Specifically how the ((x-1)**0.5) bit works out to evaluate a prime? Am I reading this to mean the integer of the square root of (x-1) - is this correct or am I missing something?


16 can be 1 x 16, 2 x 8, 4x 4, 4 x 4, 8 x 2, 16 x 1. The factor pairs do repeat themselves, so you do not need to divide all the way to the number you are testing. I am not sure if I explained it properly but it worked for me.


I follow what you just said, but I still don't follow how that applies to the specific piece of your code which I interpret to mean: the int value of the square root of (x-1).

For example, let's say x = 20. Your code appears to me to take the int value of the square root of 19, which is int(4.358.....), which then is testing: for n in range(2,4):

Am I interpreting this incorrectly?


It is a well known fact that all numbers (compound numbers, that is) are the product of one or more primes. In other words, it is possible to factor any compound number to just the prime factors, so technically this test could be done with a list of primes, only, not a complete range. But that is not what this exercise is about, so we can forego this knowledge.

Given the reversal of factors

 1  2 4 8 16
 16 8 4 2 1   (times)
  all = 16

it is evident that we do not need to iterate a full range (2..x), and need only cover a maximum of half the range since no number can be divided by a number greater than half. The above shows us that we do not even have to go that high, and the square root of our number is sufficient for our range.

Take 36

 1  2  3  4  6  9  12  18 36
36 18 12  9  6  4   3   2  1
 all = 36

we can see the crossover at 6, which is the square root of 36.

It is sufficient to use only the range (2..6) to test the divisibility of a number at or around 36.


Wow, thank you both for helping. This is my first attempt at any coding so I find it fascinating. The reason why I tried using square root is that when I tried to find if 239 is a prime number using for n in range(2, int(x)), I had a count of 237
count 235
count 236
count 237
239 is a prime number
but when I tried using square root the count is significantly less
count 11
count 12
count 13
None except that 3 did not return as a prime number. So in that sense I failed. Sorry I did not mean to mislead. I shall be grateful for any help to correct my amateurish attempt. I added count to help me understand. I know that it is not required


This exercise does not expect us to use any more than a brute force approach. That's as honest as an exercise can be, and a brute force approach satisfies the lesson checker.

We are even given the luxury of accepting that our inputs will be integer. The code we are expected to write is subject to lots of criticism but that is where we begin. How do we learn to criticize our own work if we don't have some examples to begin with? This exercise gives us that. Follow the instructions, then start to study the code once your submission passes. That will be the time when going to town would be in order.


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