is_prim (It is a bug?)


#1



15. Practice Makes Perfect #6

Oops, try again. Your function fails on is_prime(4). It returns True when it should return False.

It looks like it skips my second if conditional without executing it. As a result, whatever number I input, it returns True
I have no idea why...
Any respond will be appreciated!


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

x = int( raw_input() )
print is_prime(x)
print range (2, x)  #print range so as to make sure I use the correct numbers


#2


I think you indentat else at same level of for n in range (2, x): , but should be at same level of if x % n == 0:


#3

Uh...
I want to use for/else, so that my code can stop the for loop after it tests all the numbers in the range()

Additionally, if the else is at the same level with the if, I'm afraid it will always return True because it definitely will execute the else conditional whatever the number is.

Thanks your response! ^^


#4

Thanks for your reply
So if i say this where error come from !!


#5

oh really?
Could you explain a little bit?:grin:


#6

I'm also working on this same problem. I have a different error but I think I understood the error in your code. :smile:

Well, the issue is with the above condition itself.
Python will check the above expression like this: elif (x ==2) or (3): . This doesn't check if x == 3 instead it assigns 3 to x if x is not equals to 2. So since x equals to 3, it always returns True even though you enter any other number.

Solution: Change it to this: elif x == 2 or x == 3:

This works till 25. That is where I get an error. It says 25 should return False, but returns True. I don't know how it returns True when the if x % n should equal to 0 when n becomes 5. Not sure why it gives wrong answer.


#7

@bytemaster95356 Thanks , Can you write your code so we can discuss your problem ?


#8

Actually, it worked. I hadn't properly indented the 'else' after the for loop. Corrected it and it worked. Thanks. :slight_smile:

Happy Coding. :smile:


#9

aha! that makes sense
Could you share your code with me so that I can see what happen with your code?


#10

n = int( raw_input("Please input a number: ") )
tested_number = range(2, n)
print tested_number # to vertify that I use the correct numbers

def is_prime(x):
[Tab] if x < 2:
[Tab][Tab] return False
[Tab] elif x == 2:
[Tab][Tab] return True
[Tab] else:
[Tab][Tab] for number in tested_number:
[Tab][Tab] if x % number == 0:
[Tab][Tab][Tab] return False
[Tab][Tab][Tab] break
[Tab][Tab] else:
[Tab][Tab] return True

print is_prime(n)

this is my new code
it is interesting because when I input 3, it returns True but report that this code will fail if I input 9, and after I input 9, it returns False but reports that it will fail if I input 3... a never-ending cycle...

Please help me out!


#11

n = int( raw_input("Please input a number: ") )
tested_number = range(2, n)
print tested_number # to vertify that I use the correct numbers

def is_prime(x):
[Tab] if x < 2:
[Tab][Tab] return False
[Tab] elif x == 2:
[Tab][Tab] return True
[Tab] else:
[Tab][Tab] for number in tested_number:
[Tab][Tab] if x % number == 0:
[Tab][Tab][Tab] return False
[Tab][Tab][Tab] break
[Tab][Tab] else:
[Tab][Tab] return True

print is_prime(n)

this is my new code
it is interesting because when I input 3, it returns True but report that this code will fail if I input 9, and after I input 9, it returns False but reports that it will fail if I input 3... a never-ending cycle...

Please help me out!


#12

I got a similar error in mine, where it was skipping a conditional. The indentation wasn't on the same level, and i fixed it by changing the indentation. Also such an error shouldn't occur unless if you're using more than two conditionals, and there is a way using while loops so if you're facing trouble, try using while loops.


#13

You shouldn't need the elif bit, and the indentation for the if statement in the for loop seems wrong. Also, using a for loop isn't the best way in this case, and i don't know how it would work... anyway, try using a while loop.

If you read the instructions, you can check all of the numbers below x and above 1 if they go into x. So, you can reverse this and create a variable that is one less than x. Then, check if this new variable goes into x. If not, subtract one and try again. Repeat until the variable is 2. If no numbers succeeded, the number is prime.

Try this! It's how mine worked. I know i might not have known why the error occurred, but i tried to explain a way that DOES work. Last thing, i don't know if it was your INDENTATION that caused the error (probably wasn't) or what it was. I've only just gone on codecademy again after 16 weeks of being idle, so likely forgot an important detail or something. I'd appreciate a response to see if my support helped. Thank you.


#14

I tried While loop before this and it seemed wasn't as handle as the For loop
But since you said that, I will definitely stick to use While loop in this case
Thanks your advise, it is very concrete!