6.is_prime help plz


#1
> def is_prime(x):
>     pn = 0
>     A = True
>     if x<=1:
>         A = False
>     elif x==2:
>         return A
>     else:
>         for n in range(2,x-1):
>             pn = x % n
>             if pn == 0:
>                 A = False
>                 break
>             else:
>                 return A
>     return A

why does this return to True at 9?


#2

try without the - 1 in range.


#3

It's work for me

def is_prime(x):
if x<2:
return False
for n in range (2,x):
if x%n==0:
return False
return True
a=input('Enter max value')
for i in range(1,a+1):
if is_prime(i)==True:
print i,


#4

It doesn't work for me...


#5

I think the problem is in your wrong 'else' position. lets input '9' and see how it will goes through your code.
for n in range(2,x-1): #took '2' as first index in range
pn = x % n #pn = 9%2
if pn == 0: #looks like pn will be pn=1, so "if" doesnt work -> it goes to your "else"
A = False
break
else: #and it returns True
return A

Try to find right position for your 'else'. Good luck)
p.s. sorry for bad english


#6

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


#7

def is_prime(x):
pn = 0
if x<=1:
A = False
elif x==2 or x == 3:
A=True
else:
for n in range(2,x-1):
pn = x % n
if pn == 0:
A = False
break
else:
A = True
return A

check the coed difference


#8

Guys could you put your codes into '`'?
It's harder to understand some comments than task.


#9

Hello kzrt,

The reason it does not work is because 9 % 2 by passes if goes to the else statement and returns True. Hence the error. Please try my code with the appropriate indentation:
def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
elif x > 2:
count = 0
for n in range(2,x):
if x % n == 0:
count = count + 1
else:
count = count
if count > 0:
return False
else:
return True
else:
print "I love this stuff"


#10

Look at your loop.
What happens in the first iteration?
It will either break, or it will return, neither of which allows a second iteration.
So you have a loop that does ONE iteration. Is that really a loop? Do you need a loop?
If you do need a loop, then you'll have to leave open the possibility for it to enter next iteration. You should abort the loop as soon as you know the number is not prime, otherwise you should let the loop continue.


#12

saxoza, did this work for you?


#13

Didn't work for me. Spits back a prime(2) error of False when it should return True.


#14

def is_prime(x):

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

#15

why can't you do range(2, x-1)?


#16

Why do you need to return True in the else statement?


#17

That's the simplest working solution!

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

#18

Is there a solution to this lesson anywhere on here? I'm doing fairly well in this course and I'd just like to move along so I can learn more. I can always come back and learn this one later.


#19

Thanks for this. Worked for me. It helps if you run through the instructions carefully with this code and it will all make sense how it works.


#20

for n in range(2,sqrt(x)) is better but isn't work in this course


#21

This code worked for me: