'return' outside function error, rewriiten the code, but making no headway

Viewed 28

I'm getting the syntax error, that return is outside the func. I have rewritten the code but the problem persists. What am I doing wrong?

def prime_1(mn):
  if mn < 2:
         return 0
    
prime_in = [2] 
x = 3
while x <= mn:
    for y in prime_in:
        if x % y == 0:
            x += 2
            break
    else:
        prime_in.append(x)
        x += 2
print(prime_in)
return len(prime_in) 

    
2 Answers

Check the indentation of your return statement. The first statement has too much whitespaces in front of it.

I'm voting to close this question, but since the edit queue is full...your issue is simply syntax. Format your code correctly and you are done:

def prime_1(mn):
  if mn < 2:
    return 0
    
  prime_in = [2] 
  x = 3
  while x <= mn:
    for y in prime_in:
      if x % y == 0:
        x += 2
        break
      else:
        prime_in.append(x)
        x += 2
  print(prime_in)
  # returns in function now
  return len(prime_in) 

Related