Is_Prime help


#1



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


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


compared my answer against solutions provided, cannot understand what I am doing incorrectly. It simply isn't registering any of the code in the for loop:/


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


#2

Two improvements.

1.Add an if clause before your for loop,that checks if x == 2 and Returns True as its a prime number.

Reason:
Your current code does not test for 2.

2.This block of code...

for n in range(2, x):
        if x % n == 0:
            return False
        else:
            return True

suppose x is 9..
when you loop over it...
n is looping on range(2,9) which is a list... [2,3,4,5,6,7,8]
at first loop n takes value of 2 and as 9 is not divisible by 2, The test x % n == 0 turns out to be False and else code executes and it returns True but It had to return False

So you can do two things..
1.Either you should delete else clause and just return True or..
2.Use for/else Clause by indenting else clause as same level as for loop


#3

thanks! made the changes and was able to pass the lesson:)


#4

Cheers! :thumbsup: :slight_smile:


#6

Thanks!!, you helped me too.


#7

Than you, helped me too :slight_smile:


#8

This helped me a lot thank you, my question is how does moving the else clause into the same indentation as the for loop solve this? I can't wrap my mind around that...

Also you mention removing the else clause entirely and leaving the return True statement. How would that look/work?


#9

else clause into the same indentation as the for loop solve this?

Back in previous exercise we have learned about for/else statement.
Generally we use for/else with conjunction with break statement.
But as here we have return so we do not have to worry much. (as break and return both acts as final exit door)

If a number in not prime then for loop keep goes on and at one point it finds that
x % n == 0 evaluates True so it executes the if clause body by returning False.
but suppose a number is a prime,then it tests for all conditions when it does not find
x % n == 0 evaluated to True ,it just run else part of for loop by returning True.

Also you mention removing the else clause entirely and leaving the return True statement. How would that look/work?

Actually this one is obvious even it looks weird.

These two blocks..

 if x < 2:
        return False
    for n in range(2, x):
        if x % n == 0:
            return False

(there should one conditional statement there for prime number 2)
These two blocks actually checks if a number is not a prime,If our number does not satisfies these criteria of Non-prime number.
It simply means that our number is a prime so we should return True,we can simply add a return statement returning True at the last.

I hope it helps.


#10

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