Is_prime Practice Makes Perfect is_prime(2)


#1

I have some trouble with this "prime" problem. Can you please explain why the is_prime(2) shows None with the code i wrote.Also can you tell me if there is a better way to write the code. I don't want to see other examples because I want to improve mine. Thank you and have a nice day.

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

#2

purely looking at x=2, which return keyword do you think it reaches? And why?

negative numbers are not prime numbers


#4

okay, so now x=2 is solved. But now you have a problem with 9

Any idea why? Here is a hint: a function ends the moment a return keyword is reached.


#5

it returns None because range(2,2)= [] so i need to make a particular case for 2. i did it but now when i reach is_prime(9) it shows true even if the answer is false #newcode:
def is_prime(x):

if x>2:

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

            return False



        else:
            return True
elif x in range(0,2):
    return False
else : # for the time when x is 2
    return True

#6

i saw your post before you deleted it, so i already answered. What do you think is causing the problem for x=9?


#7

the code ends when it reads 9%2. because of that 9 is considered prime. i need to make a modification that keeps running the condition until the last number.i need a loop instead the for?


#8

for x=9 we have the following:

for n in range(2, 9):

so, the first number to be evaluate by the if condition is n=2, agree?

if 9 % 2 == 0

which is false. when If is false, else will run, which mean True will be returned, a return keyword means the function ends, the loop simply breaks


#9

even if here is 11.42 PM i will not go to sleep until i solve this bug XD


#10

your loop ends in the first iteration, because a return keyword is reached (either True or False)

When is a number a prime number? What condition is required (involving the loop)?


#11

x is prime if x% (EVERY number between 2 and x-1)==0


#12

which is currently not happening, do you understand why?


#13

i understand the the loop ends at 2 when if should check until the end. should i use another kind of loop? or i should try to modify it?


#14

you should modify the loop so that true is only returned after the loop has finished running


#15

MAN I got it. Thank you so much sir for helping. You just made my day(or my night XD).By the way.I'm Ovid. NIce to meet you
how do you upload the code as it is in chat?


#16

what do you mean? No need to upload your code

Good you solved it :slight_smile:


#17

when i upload the code is all lined up without the spaces and is hard to read.like this:
def is_prime(x):
if x>2:
for n in range(2,x):
if x%n==0:
return False
return True #at the end.just as you said
elif x <2:
return False
else : # for the time when x is 2
return True


#18

you need to use markup:

in order for it to show nicely.


#19

Thank you so much. I have the exactly same problem as corugaovidiu. :slight_smile:


#20