2013-11-21 37 views
0

我使用select.select()而不是輸入,因爲我想要輸入超時。我現在用的是「結束」的說法與print()函數,因爲我希望我的終端有這樣一行:Python3:在打印中使用「select.select」(str,end ='')

類型>TYPE的東西在這裏

相反,我沒有看到「類型> 「直到我輸入一個字符串並按回車。

我的代碼:

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS 
import sys, select 

print('Type > ', end=" ") 
INPUT, VOID0, VOID1 = select.select([sys.stdin], [], [], 3) 

if (INPUT): 
    print('You said, ' + sys.stdin.readline().strip()) 
else: 
    print('You said nothing!') 

我使用這個腳本來測試select.select()和print(海峽,終點=」「)。我閱讀了這篇文章(How can I suppress the newline after a print statement?)和兩個命令的官方Python3文檔。

回答

1

stdout默認情況下緩衝,以迫使它顯示你需要清理一下:

print('Type > ', end='') 
sys.stdout.flush() 

注意print也通過關鍵字參數支持這一點:

print('Type > ', end='', flush=True) 
+0

謝謝!這很好。直到七分鐘過去後,我無法回答你的答案。我會在瀏覽器上打開此選項卡,以便標記您的答案。 –

+0

@DevynCollierJohnson無後顧之憂 - 我認爲,這不是巧合,我得到了一個答案與選擇聲明upvote然後這個問題變成了:) –

相關問題