Valid code is failed because it can't process 0, even though that is not needed


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096


I get the following error message when submitting the code.
"Oops, try again. Your function fails on is_prime(0). It returns True when it should return False."


The code does not give any error messages when executed in an external environment, so there are no syntax errors.

That the code cannot process 0, is not really relevant. See the introduction to the exercise:

is_prime
"A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself..."

So, the premise clearly stated is integer numbers 2 or higher. Thus the function does not need to be able to process 0, -3 or 4.67. Furthermore, the exact instructional steps 01 - 04 talks about checking if the number can be divided, assuming again an integer 2 or higher. There is no mentioning that the code should check for wrong input. Thus, failing code that cannot process numbers lower than 2 or even decimals for that matter is not really valid.


def is_prime(x):
    for y in range(2, x):
        if x % y == 0:
            return False
            break
    else:
        return True


#2

That 0 and 1 aren't prime doesn't mean that your function can ignore theses cases. If you assume that the input is always prime than the entire exercisse is redundant and you could just write:

`

If you wanted to be very stright the function should also check if x is an integer at all, but note that your function doesn't return an error for zero and one, it returns True. For non integer x there would be an error because range(2, x) is only defined for integer x.


#3

Thanks for your reply.

I do see what you mean in regards to returning a wrong true value for other numbers. I guess my point is that these do not even need to be considered at all, just like decimals. It is inherent in the definition of a prime number.

To me the exercise, as explained in the introduction and the exact instructional steps, was to test if a number, which could be a prime number, really was one -- and not if someone input values that could not possibly be prime numbers per the basic definition. I was therefore a bit baffled by the error message.

I do not think that the exercise would be redundant if I were to only consider integers from 2 onward. The division and check of the remainder is the exercise. Someone making a program to find prime numbers would not start from -10000 but from 2.

I like to argue and probably you do as well :slight_smile:. Peace be with it. Its not a big deal.
To get on with the course, I did enter an extra if statement in the beginning returning false for numbers less than 2.

Have a great weekend!


#4

That's why your function should not return True for these numbers.

If this person was worried about avoiding unnecessary checks he should probably worry more about the divisibility test. It's only necessary to check up to the square root of the prime candidate.

A nice weekend to you too :relaxed:


#5

Yes, and I did actually change the code to limit the division when testing in the external environment as well as use the function for numbers up to 25 to see that it worked, but tried to follow the instructions in the actual exercise, thinking that the code might not be accepted, if I did not try out all the possibilities.

Again, thanks for taking the time to reply.

I am having a great time with this course. The codecademy course form is very good for fast learning a new language with just the right mix of theory and practice to make the knowledge stick.

Having programmed for many years in different languages, I must say that I really appreciate the elegance, simplicity, legibility of Python. It is so refreshing not having to read huge tomes about about using this or that kind of variable, array, list, etc. because of heap, stack and performance, when in many use cases it does not really matter that much and getting the job done should be the focus rather than a lot of complicated compiler internals.


#6

i> s_prime

"A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself..."

So, the premise clearly stated is integer numbers higher than 1 Thus the function does not need to be able to process 0, -3 or 4.67.

if you know these are not prime numbers then return False if they are provided.


#7

Yeah, and for decimals and other invalid input as well.


#8

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