Is_prime


#1



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


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


I expected my function to return False,indicating that 4 is not a prime number. But instead it keeps returning True.

I have divided my function into four parts:
first part checks if it the number is greater than 1 (because all integers less than 2 are not prime numbers)
second part checks if modulo of the number divided by one and itself is equal to 0 (which is required for prime numbers)
third part should check if modulo of any number in the range (2,the number) is 0, if so, it should return False, indicating the number is not a prime number
Fourth part checks if all three conditions are met.

Yet, there seems to be an issue in the third part, I think.


def is_prime(x):
    if x > 1: 
        req1 = True
    else: 
        req1 = False 
    return req1
    if (x % 1 == 0) and (x % x == 0): 
        req2 == True 
    else: 
        req2 == False 
    return req2
    y = range(2,x-1)
    req3 = True
    for i in y:
        if x % i == 0: 
            req3 = False 
    return req3
    if (req1 == True) and (req2 == True) and (req3 == True):
        return True
    else: 
        return False

is_prime(4)


#2

I found out my mistake.

in condition 2 (second if), I mistakenly put a comparator == instead of =

the returns under each condition were writing True or False as an answer to my function, i should have deleted them.

also i forgot to check if X == 0 in the beginning, because if so, the modulo crashes.

The corrected version of this code is:

def is_prime(x):
if x == 0:
return False

if x > 1: 
    req1 = True
else: 
    req1 = False 

if (x % 1 == 0) and (x % x == 0): 
    req2 = True 
else: 
    req2 = False 

y = range(2,x-1,1)
req3 = True
for i in y:
    if x % i == 0: 
        req3 = False 

if (req1 == True) and (req2 == True) and (req3 == True):
    return True
else: 
    return False

is_prime(4)


#3

Some comments, mostly on redundancy and readability


You don't need to continue testing if you've already figured out that it's not a prime

Comparing True to True is going to result in True, skip the comparison and use the value directly instead

likewise, if you have a boolean expression, don't test its value with an if-statement to determine which boolean to return, return the value directly instead. Use not if it should be inverted

A value that's greater than 1 isn't 0, don't need to test for 0 - you already do

range's default step is already 1, no need to specify it

range's stop argument is exclusive, if you give it a stop argument of 1 less than x then the last value will be 2 less

When code starts getting long and the variable names don't say anything about what's going on, add short comments saying what's being done or you'll end up having to re-read things to figure out what they are

boolean variables are usually named after the thing they represent the truth of, like is_greater_than_1, not req1

names like x y and i make it very difficult to tell what's what

you're only using y's value once, there's no need to create that variable, especially when it doesn't describe what it represents in any way - the range expression is far more descriptive than "y"

If you don't know what's going on then find out - add some prints to allow the program to describe/motivate each step it's doing, you can then read the output to find out what happened


#4

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