Python SyntaxError: 'return' outside function

Asked 2 years ago, Updated 2 years ago, 46 views

i2 = np.random.randint(3)
j2 = np.random.randint(3)
a1 = np.eye(3)[i2][j2]
a2 = np.eye(3)[j2][i2]

if (a1 == a2) :
 return True
else :
  return False

If i2 and j2 change positions through unspecified parts i2 and j2 in the matrix, I want to show a false return if the True return is wrong, but when I entered it as it is in the cobap, the error SyntaxError: 'return' outside function appears. What's the problem? You can print it out and not just return the value. Indent is in the right state.

python numpy

2022-09-20 14:32

1 Answers

return is the command used by the function.

We need to modify it in the following way.

i2 = np.random.randint(3)
j2 = np.random.randint(3)
a1 = np.eye(3)[i2][j2]
a2 = np.eye(3)[j2][i2]

def a1a2():
    if (a1 == a2) :
        return True
    else :
        return False
i2 = np.random.randint(3)
j2 = np.random.randint(3)
a1 = np.eye(3)[i2][j2]
a2 = np.eye(3)[j2][i2]

print(a1 == a2)


2022-09-20 14:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.