How Do You Escape or Break from a Method in Python?

Problem scenario
You want to quit a function, or return out of it, when a certain condition is met. How do you do this?

This program illustrates how it is done. You will want to run the program twice, once by leaving the "5" as it is, and a second time by changing the "5" to "6".

def cool(x):
  if x == 5:
    return "There was equivalence"
    y = x
  print("This statement is printing as an illustration")
  return "The comparison was NOT equivalent"

var1 = cool(5)

Leave a comment

Your email address will not be published. Required fields are marked *