Stop when get input in Python
How开发者_运维问答 can I run a loop in Python and code it to stop when the user press a button (not ctr+c)?
I had a similar problem, found this code excerpt that helped me.
#!/usr/bin/env python
import curses
def main(win):
win.nodelay(True) # make getkey() not wait
x = 0
while True:
    #just to show that the loop runs, print a counter
    win.clear()
    win.addstr(0,0,str(x))
    x += 1
    try:
        key = win.getkey()
    except: # in no delay mode getkey raise and exeption if no key is press 
        key = None
    if key == " ": # of we got a space then break
        break
#a wrapper to create a window, and clean up at the end
curses.wrapper(main)
The problem of course is that using curses will prevent a lot of other things (like print) from working but there are ways around that.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论