About 57 results
Open links in new tab
  1. python - How can I break out of multiple loops? - Stack Overflow

    There is no way to do this from a language level. Some languages have a goto others have a break that takes an argument, python does not. The best options are: Set a flag which is checked by the outer loop, or set the outer loops condition. Put the loop in a function and use return to break out of all the loops at once. Reformulate your logic ...

  2. python - How to stop one or multiple for loop (s) - Stack Overflow

    Use break and continue to do this. Breaking nested loops can be done in Python using the following: for a in range(...): for b in range(..): if some condition: # break the inner loop break else: # will be called if the previous loop did not end with a `break` continue # but here we end up right after breaking the inner loop, so we can # simply break the outer loop as well break

  3. loops - Why "if-else-break" breaks in python? - Stack Overflow

    If is an expression, break similar to return is a statement. You can't use two statements in a single sentence (unless you use a semicolon which is ugly). I know it would have been really cool if we can do that, but alas that's the way it is.

  4. How can I do a line break (line continuation) in Python (split up a ...

    In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested. [3]: Donald Knuth's The TeXBook, pages 195 and 196

  5. How to break out of nested loops in python? - Stack Overflow

    Nov 15, 2016 · A break will only break out of the inner-most loop it's inside of. Your first example breaks from the outer loop, the second example only breaks out of the inner loop. To break out of multiple loops you need use a variable to keep track of whether you're trying to exit and check it each time the parent loop occurs.

  6. Python Break Inside Function - Stack Overflow

    Sep 20, 2016 · I am using Python 3.5, and I would like to use the break command inside a function, but I do not know how. I would like to use something like this: def stopIfZero(a): if int(a) == 0: b...

  7. Is it a bad practice to use break in a for loop? - Stack Overflow

    It's perfectly valid to use break - as others have pointed out, it's nowhere in the same league as goto. Although you might want to use the vFound variable when you want to check outside the loop whether the value was found in the array. Also from a maintainability point of view, having a common flag signalling the exit criteria might be useful.

  8. How to break out of while loop in Python? - Stack Overflow

    Nov 11, 2024 · Don't use while True and break statements. It's bad programming. Imagine you come to debug someone else's code and you see a while True on line 1 and then have to trawl your way through another 200 lines of code with 15 break statements in it, having to read umpteen lines of code for each one to work out what actually causes it to get to the break.

  9. python - How to exit an if clause - Stack Overflow

    There is another way which doesn't rely on defining functions (because sometimes that's less readable for small code snippets), doesn't use an extra outer while loop (which might need special appreciation in the comments to even be understandable on first sight), doesn't use goto (...) and most importantly let's you keep your indentation level ...

  10. python - What commands are alternatives to "break"? - Stack …

    Jun 21, 2021 · A simple solution available on Python 3.8+ is to use the walrus operator (assignment expression for boring types) and just have the loop run until the result is valid, removing the handling for valid inputs from the loop entirely, putting them outside/after the loop entirely, so for your specific problem the end result would look like:

Refresh