def foo(x): for i in range(0, x): try: ... except* TypeError: if x == 1: return 1 if x == 2: continue if x == 3: break