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(9). It returns True when it should return False.


DO pass


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


#2

I don't understand what's wrong!


#3

a function ends the moment a return keyword is reached

so for x=9, the first number tested by the loop is 2:

if 9 % 2 == 0

which is false, so else runs. True is returned, the function ends (so does the loop)

but for a number to a prime number, the number must finish running and return True

Place return True outside the loop

If the number is then not a prime number, the if condition will evaluate to True at some point, False will be returned and the function ends. Which is good, which is what you want


6 is_prime
#4

I needed to understand the logic behind the problem before understanding how to program it, so I will try to help you out the best I can. The question asks us to define whether or not a number is prime by running it through a set of conditions. Your first two conditions are good. On your third, when you run 9 through it, it stops after one iteration because you return false. Personally, I think a for loop is a bad way to go about this. So, delete everything after "else:"...

Try a while loop and define your variables. The "n" value is the number you divide x (or 9 in this case) by. Therefore, you should set n = n-1 (otherwise it will divide 9/9 and the if statement will return True immediately, saying that 9 is a prime number, when it is not). After defining n, write your while statement. You can do this a number of ways but when I wrote my statement, I had the iterations loop over while n > 1. Therefore, if x = 9, n = 8, and it will loop until n=1.

Once we have the while loop written, we can get to the conditions. You can keep the statement "if x % n == 0: return false". After that we need an else statement that ensures we iterate through every number 9 could be divided by. When you write this else statement, do not include a return statement with it, just update the n value. It should be n= n -1.

Finally, you need a return statement that will return a value after the loop finishes running.


#5

A for loop is a fine way to solve this problem? You can simply use for/else, switching solution might be confusing for learner, they gotten pretty far with there logic (in fact, in this case the learner is very close), too make this many changes might be confusing. We will see what the learner picks


#6

I think I understand what you are saying, but I don't know what put return True outside the Loop means. Should I put the whole Else: statement outside the loop or just Return True outside the loop. Please tell me how to do that. Thanks!


#7

i didn't say on purpose, because you can do both? you can get rid of the else and simply place return True outside the loop or place else outside the loop so you have for/else, then else will run when loop condition becomes false

i wanted you to choice which one you understood and felt comfortable with


#8

Thank you soo much it worked !!!!


#9