Is_prime issue


#1



Stuck at is_prime, put a counter to control the process, but


Here is a problem popped out...

File "python", line 3
SyntaxError: 'return' outside function


x = int(raw_input('Enter:'))
if int(x) < 2 :
    return False
else:
    def is_prime(x):
        counter = 0
        for item in range(2, (x)):
            if x % item == 0:
                counter += 1
                print "+1"
            else:
                counter += 0
                print "+0"
        print counter
        if counter != 0:
            return False
        return True
    print is_prime(x)


#2

its beacause of that the application wrote to exmaine your code expect you to write return just in function and doesnt prompt you to use it outside of the function


#4

Cheers, man!
Already fixed:slight_smile:


#5

Hi, just sharing my solution that worked:
def is_prime(x):
if x < 2:
return False
if x == 2:
return True

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

#6

literally copypastaing your code (and fixing spacing) fails for 3. if i remove the "-1", it fails for 9. any help?


#7

Sorry if I'm too late for this, but that was not a spacing error on his part. That was part of a for/else construction, i believe.


#8

same with me. Need help


#9

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