2012-02-02 80 views
2

我該如何編寫一個總是在尋找用戶輸入的Python程序。我想我會想要一個變量等於輸入,然後根據變量等於什麼發生不同的事情。所以如果變量是「w」,那麼它會執行一個特定的命令,並繼續這樣做,直到它接收到另一個輸入如「d」然後會發生一些不同的事情,但它不會停止,直到你輸入。不斷在Python中尋找用戶輸入

+3

我不明白你想要達到的目標,但是如果你需要很好的控制用戶插入數據的方式,你可能需要一個GUI應用程序...... – 2012-02-02 01:48:25

+0

不希望GUI應用程序,這是一個研究項目沒有應用開發。 – cbbcbail 2012-02-02 01:51:12

回答

4

如果你想不斷尋找一個用戶輸入你需要multithreading

例子:

import threading 
import queue 

def console(q): 
    while 1: 
     cmd = input('> ') 
     q.put(cmd) 
     if cmd == 'quit': 
      break 

def action_foo(): 
    print('--> action foo') 

def action_bar(): 
    print('--> action bar') 

def invalid_input(): 
    print('---> Unknown command') 

def main(): 
    cmd_actions = {'foo': action_foo, 'bar': action_bar} 
    cmd_queue = queue.Queue() 

    dj = threading.Thread(target=console, args=(cmd_queue,)) 
    dj.start() 

    while 1: 
     cmd = cmd_queue.get() 
     if cmd == 'quit': 
      break 
     action = cmd_actions.get(cmd, invalid_input) 
     action() 

main() 

正如你會看到這一點,就會得到你的消息有點混了,是這樣的:

> foo 
> --> action foo 
bar 
> --> action bar 
cat 
> --> Unknown command 
quit 

這是怎麼一回事,因爲有兩個線程寫在給stdoutput同時。要同步他們那裏將是需要的lock

import threading 
import queue 

def console(q, lock): 
    while 1: 
     input() # Afther pressing Enter you'll be in "input mode" 
     with lock: 
      cmd = input('> ') 

     q.put(cmd) 
     if cmd == 'quit': 
      break 

def action_foo(lock): 
    with lock: 
     print('--> action foo') 
    # other actions 

def action_bar(lock): 
    with lock: 
     print('--> action bar') 

def invalid_input(lock): 
    with lock: 
     print('--> Unknown command') 

def main(): 
    cmd_actions = {'foo': action_foo, 'bar': action_bar} 
    cmd_queue = queue.Queue() 
    stdout_lock = threading.Lock() 

    dj = threading.Thread(target=console, args=(cmd_queue, stdout_lock)) 
    dj.start() 

    while 1: 
     cmd = cmd_queue.get() 
     if cmd == 'quit': 
      break 
     action = cmd_actions.get(cmd, invalid_input) 
     action(stdout_lock) 

main() 

好了,現在它的更好:

# press Enter 
> foo 
--> action foo 
    # press Enter 
> bar 
--> action bar 
    # press Enter 
> cat 
--> Unknown command 
    # press Enter 
> quit 

請注意,你需要輸入一個命令,在「輸入模式進入前壓Enter 」。

2

http://www.swaroopch.com/notes/Python_en:Control_Flow

#!/usr/bin/python 
# Filename: while.py 

number = 23 
running = True 

while running: 
    guess = int(input('Enter an integer : ')) 

    if guess == number: 
     print('Congratulations, you guessed it.') 
     running = False # this causes the while loop to stop 
    elif guess < number: 
     print('No, it is a little higher than that.') 
    else: 
     print('No, it is a little lower than that.') 
else: 
    print('The while loop is over.') 
    # Do anything else you want to do here 

print('Done') 
+0

所以你試圖說我可以使用這段代碼的基礎知識(就像它的兩行一樣),並改進它,直到它做我想要的東西? – cbbcbail 2012-02-02 01:54:33

+0

是的,應該是一個很好的開始。 – 2012-02-02 01:56:30

-1

您還可以使用的定義,比方說,像這樣:

def main(): 
    (your main code) 
    main() 
main() 

雖然一般while循環,也乾淨多了,不需要全局變量:)

+1

這是純粹的邪惡 – 2012-02-02 02:05:05

+0

不能反駁這個;) – CR0SS0V3R 2012-02-02 02:07:50

1

也許select.select是你正在尋找的東西,它檢查是否有數據準備好在文件描述符中讀取,所以你只能閱讀在哪裏它避免了中斷處理(當然,在本例中它等候一秒,但替換1 0,它會很好地工作):

import select 
import sys 

def times(f): # f: file descriptor 
    after = 0 

    while True: 
     changes = select.select([f], [], [], 1) 

     if f in changes[0]: 

      data = f.readline().strip() 

      if data == "q": 
       break 

      else: 
       print "After", after, "seconds you pressed", data 

     after += 1 

times(sys.stdin) 
+0

也許解釋什麼'select'呢?編輯 – Marcin 2012-02-02 17:09:28

+0

,感謝您的提示 – kenkeiras 2012-02-03 01:03:52

0

如果你想從用戶重複輸入;

x=1 
while x==1: 
    inp = input('get me an input:') 

並基於inp您可以執行任何條件。