2013-07-31 89 views
3

我一直在瀏覽很長時間來搜索對此的答案。退出while循環與用戶提示在python中

我在Unix中使用Python 2.7。

我有一個連續的while循環,我需要一個選項,用戶可以中斷它,做一些事情之後,循環會繼續。

像:

while 2 > 1: 
    for items in hello: 
     if "world" in items: 
      print "hello" 
     else: 
      print "world" 

     time.sleep(5) 
     here user could interrupt the loop with pressing "u" etc. and modify elements inside he loop. 

我開始測試的raw_input出來,但因爲它提示我每循環,它的東西,我不需要。

我想在這裏提到的方法:

Keyboard input with timeout in Python

幾次,但沒有這些似乎工作我多麼希望。

+1

你希望如何工作?我想,那時你只有一個詞:線程。 – freakish

+3

爲什麼你會用'while 2> 1'而不是'while True'? – geoffspear

+0

'raw_input()'不會讀取一個字符,因爲「普通」控制檯輸入是行緩衝的。看到這個配方非緩衝閱讀http://code.activestate.com/recipes/134892/ – WGH

回答

0

你可以做嵌套while循環,一些結構是這樣的:

while true: 
    go = True 
    while go: 
    for items in hello: 
     if "u" in items: 
      go = False 
     else if "world" in items: 
      print "hello" 
     else: 
      print "world" 
    #Here you parse input to modify things in the nested loop, include a condition to set  
    #go back to true to reenter the loop      
+0

Python是一種區分大小寫的語言。沒有定義「真」和「假」。他們應該是「真」和「假」。 – iCodez

+0

哎呀,我傻了,我一直在看C#的時間太長了 –

+0

這個遍歷「hello」並從那裏搜索「u」,這不是問題。 – jester112358

4
>>> try: 
... print 'Ctrl-C to end' 
... while(True): 
...  pass 
... except KeyboardInterrupt, e: 
... print 'Stopped' 
... raise 
... 
Ctrl-C to end 
Stopped 
Traceback (most recent call last): 
    File "<stdin>", line 2, in <module> 
KeyboardInterrupt 
>>> 

顯然,你需要更換,不管你在做通和善後打印。

0
import sys 
import os 
import time 

import termios 
import tty 
import fcntl 
import errno 

KEY = "u" 

def read_characters(f): 
    fd = f.fileno() 

    # put descriptor in non-blocking mode 
    flags = fcntl.fcntl(fd, fcntl.F_GETFL) 
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 

    try: 
     while 1: 
      try: 
       yield f.read(1) 
      except IOError as e: 
       if e.errno == errno.EAGAIN: 
        # no more characters to read 
        # right now 
        break 
    finally: 
     # restore blocking mode 
     fcntl.fcntl(fd, fcntl.F_SETFL, flags) 

def main(): 
    fd = sys.stdin.fileno() 

    # save current termios settings 
    old_settings = termios.tcgetattr(fd) 

    # tty sets raw mode using termios module 
    tty.setraw(fd) 

    try: 
     while True: 
      time.sleep(1) 

      for c in read_characters(sys.stdin): 
       if c == KEY: break 
      else: 
       c = None 

      if c == KEY: break 

      sys.stdout.write("still going\r\n") 

    finally: 
     # restore terminal settings 
     termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 

if __name__ == "__main__": 
    main() 
2

下面介紹如何通過查詢標準輸入做到這一點:

import select, sys 
p = select.poll() 
p.register(sys.stdin, 1) #select the file descriptor, 
      #in this case stdin, which you want the 
      #poll object to pay attention to. The 1 is a bit-mask 
      #indicating that we only care about the POLLIN 
      #event, which indicates that input has occurred 

while True: 
    for items in hello: 
     if "world" in items: 
       print "hello" 
     else: 
       print "world" 

    result = p.poll(5) #this handles the timeout too 
    if len(result) > 0: #see if anything happened 
     read_input = sys.stdin.read(1) 
     while len(read_input) > 0: 
       if read_input == "u": 
        #do stuff! 
       read_input = sys.stdin.read(1) #keep going 
          #until you've read all input 

注:這可能不會在Windows上工作。