Is_prime issue


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
    def is_prime(x):
        counter = 0
        for item in range(2, (x)):
            if x % item == 0:
                counter += 1
                print "+1"
                counter += 0
                print "+0"
        print counter
        if counter != 0:
            return False
        return True
    print is_prime(x)


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


Cheers, man!
Already fixed:slight_smile:


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
    return True


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


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.


same with me. Need help


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