我該如何編寫一個總是在尋找用戶輸入的Python程序。我想我會想要一個變量等於輸入,然後根據變量等於什麼發生不同的事情。所以如果變量是「w」,那麼它會執行一個特定的命令,並繼續這樣做,直到它接收到另一個輸入如「d」然後會發生一些不同的事情,但它不會停止,直到你輸入。不斷在Python中尋找用戶輸入
回答
如果你想不斷尋找一個用戶輸入你需要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
」。
從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')
所以你試圖說我可以使用這段代碼的基礎知識(就像它的兩行一樣),並改進它,直到它做我想要的東西? – cbbcbail 2012-02-02 01:54:33
是的,應該是一個很好的開始。 – 2012-02-02 01:56:30
您還可以使用的定義,比方說,像這樣:
def main():
(your main code)
main()
main()
雖然一般while循環,也乾淨多了,不需要全局變量:)
這是純粹的邪惡 – 2012-02-02 02:05:05
不能反駁這個;) – CR0SS0V3R 2012-02-02 02:07:50
也許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)
如果你想從用戶重複輸入;
x=1
while x==1:
inp = input('get me an input:')
並基於inp您可以執行任何條件。
- 1. Python,不斷提示用戶輸入
- 2. 代碼不終止並不斷尋找輸入
- 3. 在閱讀Python中的用戶輸入時生成中斷
- 4. 中斷Python中的輸入
- 5. VS2010不斷尋找東西
- 6. Nginx不斷尋找index.html
- 7. 根據用戶輸入尋找數組中的最大值(C#)
- 8. 用戶輸入被截斷
- 9. LAN架構 - 尋找輸入
- 10. 中斷用戶輸入,否則繼續
- 11. 在python中尋找質數
- 12. 在Python中查找用戶輸入的重複值
- 13. 用戶輸入在循環中不斷提問C++
- 14. 在Python中解析用戶輸入
- 15. 在Python中令牌化用戶輸入
- 16. 在python中翻譯用戶輸入
- 17. 在沒有輸入或非數字輸入時的用戶輸入中斷
- 18. 的Java如何尋找SQL從用戶輸入方法
- 19. 在python中用二進制搜索樹輸入用戶輸入
- 20. Python不斷解析控制檯輸入
- 21. 如何從python中的用戶輸入中找到關鍵字?
- 22. 如何找出用戶在文本輸入中按下「輸入」?
- 23. Python不會接受用戶輸入
- 24. 查找用戶輸入
- 25. Squishit不斷尋找AjaxMin版本4.48.4489.28432
- 26. MVC EF不斷尋找昂貴的userId
- 27. 尋找與Python
- 28. python - 定時用戶輸入
- 29. Python-多用戶輸入
- 30. 的Python:從用戶輸入
我不明白你想要達到的目標,但是如果你需要很好的控制用戶插入數據的方式,你可能需要一個GUI應用程序...... – 2012-02-02 01:48:25
不希望GUI應用程序,這是一個研究項目沒有應用開發。 – cbbcbail 2012-02-02 01:51:12