Search code examples
loopsif-statementbreak

Wanna ask about breaking the loop in a defined function?


i have a defined function called x(), and I call this function inside the while loop in another defined function y(). Inside the x(), I have an if statement in it. I don't know how to code (if something is true for the if-statement in the x(), then break the while loop in y()?


Solution

  • def x():
        return True
    def y():
        while True:
            if x():
                break
        print("Out of Loop")
    y()
    

    I assume function x is returning true. if x(): this check if function return true than it will break the loop in function y(). You can return bool value in x() function and check that bool value in y() function to break the loop

    enter image description here