yield and return only make sense in the context of functions. Using them outside a function raises a
SyntaxError.
If the goal is to break out of a loop, use break instead of return.
Code examples
Noncompliant code example
a = 1
while a < 3:
    if a % 2 == 0:
        return # Noncompliant: return is outside of a function
    a += 1
for n in range(5):
  yield n # Noncompliant: yield is outside of a function
Compliant solution
a = 1
while a < 3:
    if a % 2 == 0:
        break
    a += 1
def gen():
    for n in range(5):
      yield n