So, I read a little about how to exit the while loop by pressing the enter key, and I came up with the following:
import sys, select, os switch = 1 i = 1 while switch == 1: os.system('cls' if os.name == 'nt' else 'clear') print "I'm doing stuff. Press Enter to stop me!" print i while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: line = raw_input() if not line: print "Finished! =]" switch = 0 else: print "Finished! =]" switch = 0 i = i+1
Is there any way to remove this? In particular, "if not line" and the next "else" look randomly. Is it possible to combine them into one? Best alternative to using a "switch"?
Initially, if I typed a bunch of characters, and then hit enter, it wonβt stop the loop. I would need to press a key again. The if and else components are designed to configure it so that it exits the first time you press input.
python input loops exit enter
user3394391
source share