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.

I don't know if this is mathematical or code problem

for example 9,
9
for i in [ 2, 3, 4, 5, 6, 7, 8 ]

9%2=1
9%3=0
9%4=1
9%5=4
9%6=3
9%7=2
9%8=1

9 is moduled by number 2 to 9-1 (which is limited by range (2, x))
and results include 0. Therefore, I want to make it false if there are 0 in module result.


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


#2

Add prints in your code to find out if it's really doing those operations that you wrote down. You have determined what it should do, that's good. Now find out if it does that.


#3

I went back and tried my code again for this exercise, and it gave out an error.

I didn't know how I could pass it in the first place, but I debugged it in the way of just printing all the iterations according to how the program is doing its job.

I just realized that it all comes down to the location of the action in one of the else tabs. That's enough hint :slight_smile:


#4

thank you for the help. Can you tell me where should i put print to see the operations?


#5

Make them print out the same thing that you wrote yourself:

9%2=1 
9%3=0
9%4=1
9%5=4
9%6=3
9%7=2
9%8=1

You'll need to do a bit of string formatting to produce that output


#6

def is_prime(x):
    if x == 2:
        return True
    elif x>2:
        for i in range(2,x):
            print x, '%', i, '=', x % i
            if x % i == 0:
                return False
            else:
                return True
    else:
        return False
        

print is_prime(7)
print is_prime(9)
print is_prime(1)
print is_prime(15)

OK according to your comment, i did some string formatting and watched what happens with the for loop.
And I found that my iteration stops whenever they have first module process and return value of True, they stopped the iteration. So I guess that for loop stops when they return a value (True/ False). Is this right?
So I draw out my else: return True out of for loop and the iteration continued until it returns False, or moved to else statement after the end of iteration.

def is_prime(x):
    if x == 2:
        return True
    elif x>2:
        for i in range(2,x):
            print x, '%', i, '=', x % i
            if x % i == 0:
                return False
            else:
                return True
    else:
        return False

#7

Right on the money! Your first problem at the very beginning was that at first iteration, your program checked the number to be "moduloed" with the number 2, and when the If statement doesn't work (which is what the if statement IS supposed to do in this exercise), it goes to the Else statement. That's why when 9 is given, returns True.

So now if you follow my previous hint, you can answer this problem already.


#8

Yes, you would put the thing that should only run after the loop, after the loop. You should not have an else there as you removed it from the if-statement, if you just de-indent it then you have a for-else which is a thing but since you don't know what it does is not something you should be using, at least not without finding out first. So you'd instead ask yourself how to do it with what you know (and indeed for-else makes little sense there so you wouldn't be using it anyhow)

return doesn't just cause a loop to stop, what is the purpose of a return statement, what does it do?


#9

thankyou for the help. maybe i should review what i already know and how they exactly works.


#10

I think you should remember what you can do, as in general concepts. And then when you need to actually do it, just make sure that you're never guessing about anything, instead look it up, so that you only write things that you know what they do.

So the actions that you want to perform is what matters the most, and then when you describe those actions in code you want to be sure that you know what it is you are typing. You're going to make mistakes regardless, but saying things you don't know what they mean will put the situation out of control, so no guessing!

You can't remember everything, especially not at first. Instead you need to constantly look things up, learn how to find that information. If it's syntax then you probably want to search for some tutorial on the topic, if it's the behaviour of some function, then you'll want to search for official documentation.


#11

def is_prime(x):
count = 0
if x < 2:
return False
else:
for i in range(2,x):
if x%i == 0:
count += 1
if count > 0:
print "Not prime"
return False
else:
print "Is prime"
return True

is_prime(-7)


#12

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