Fix Python – How to kill a while loop with a keystroke?

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 user presses the ‘esc’ or ‘return’ key:
break

I have done something like this using opencv, but it doesn’t seem to….

Fix Python – How can I make sense of the `else` clause of Python loops?

Many Python programmers are probably unaware that the syntax of while loops and for loops includes an optional else: clause:
for val in iterable:
do_something(val)
else:
clean_up()

The body of the else clause is a good place for certain kinds of clean-up actions, and is executed on normal termination of the loop: I.e., exiting the loop wi….

Fix Python – How to check if all elements of a list match a condition?

I have a list consisting of like 20000 lists. I use each list’s 3rd element as a flag. I want to do some operations on this list as long as at least one element’s flag is 0, it’s like:
my_list = [[“a”, “b”, 0], [“c”, “d”, 0], [“e”, “f”, 0], …..]

In the beginning, all flags are 0. I use a while loop to check if at least one element’s flag is 0:
….

Fix Python – How do I plot in real-time in a while loop using matplotlib?

I am trying to plot some data from a camera in real time using OpenCV. However, the real-time plotting (using matplotlib) doesn’t seem to be working.
I’ve isolated the problem into this simple example:
fig = plt.figure()
plt.axis([0, 1000, 0, 1])

i = 0
x = list()
y = list()

while i < 1000: temp_y = np.random.random() x.append(i) y.ap....

Fix Python – How to emulate a do-while loop?

I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:
list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None

while True:
if element:
print element

try:
element = iterator.next()
except StopIteration:
break

print “done”

Instead of “1,2,3,do….