break

If statement returning False in While True loop (Python)

If statement returning False in While True loop (Python) Question: I expected that in this If statement, the variable ‘i’ would increment until it eventually equals 10, and subsequently ‘if 10 < 10’ would return False, breaking my while loop. But this code seems print until 10 and then get stuck in an infinite loop …

Total answers: 6

continuous number increment until user input == 'specific input' Python

continuous number increment until user input == 'specific input' Python Question: First to say I am new to ‘asking questions’ in stackoverflow so I apologize if my question is not in the right category or another similar question has been asked already. I tried to find a relevant question but I couldn’t. Mine is a …

Total answers: 1

Break for loop in an if statement

Break for loop in an if statement Question: Currently having trouble with breaking this for loop. I want to break it if the variable is not found in this list so it can move two another for loop. It expects an indented block for the top of the for loop, but if I change the …

Total answers: 1

Jump from try to except using python

Jump from try to except using python Question: I searching the way to break the try and go into the except (This example has an syntaxerror in the break line) def fool(): return 0 try: var = fool() if var == 0: pass # from here jump to except except: fool2() Another way i thought …

Total answers: 5

Is there an equivalent to the "for … else" Python loop in C++?

Is there an equivalent to the "for … else" Python loop in C++? Question: Python has an interesting for statement which lets you specify an else clause. In a construct like this one: for i in foo: if bar(i): break else: baz() the else clause is executed after the for, but only if the for …

Total answers: 14

Python – `break` out of all loops

Python – `break` out of all loops Question: I am using multiple nested for loops. In the last loop there is an if statement. When evaluated to True all the for loops should stop, but that does not happen. It only breaks out of the innermost for loop, and than it keeps on going. I …

Total answers: 4

How to kill a while loop with a keystroke?

How to kill a while loop with a keystroke? Question: I am reading serial data and writing to a csv file using a while loop. I want the user to be able to kill the while loop once they feel they have collected enough data. while True: #do a bunch of serial stuff #if the …

Total answers: 17

What does a semicolon do?

What does a semicolon do? Question: I got a function online to help me with my current project and it had semicolons on some of the lines. I was wondering why? Is it to break the function? def containsAny(self, strings=[]): alphabet = ‘abcdefghijklmnopqrstuvwxyz0123456789’ for string in strings: for char in string: if char in alphabet: …

Total answers: 5

Naming Loops in Python

Naming Loops in Python Question: I recently read this question which had a solution about labeling loops in Java. I am wondering if such a loop-naming system exists in Python. I have been in a situation multiple times where I do need to break out of an outer for loop from an inner for loop. …

Total answers: 4

How can I break out of multiple loops?

How can I break out of multiple loops? Question: Given the following code (that doesn’t work): while True: # Snip: print out current state while True: ok = get_input("Is this ok? (y/n)") if ok.lower() == "y": break 2 # This doesn’t work 🙁 if ok.lower() == "n": break # Do more processing with menus and …

Total answers: 39