亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

raw_input和超時

raw_input和超時

HUWWW 2019-08-08 15:34:37
raw_input和超時我想做一個raw_input('Enter something: .')。我希望它睡3秒鐘,如果沒有輸入,則取消提示并運行其余代碼。然后代碼循環并raw_input再次實現。如果用戶輸入類似“q”的內容,我也希望它能夠破解。
查看完整描述

3 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

有一個簡單的解決方案,不使用線程(至少沒有明確):使用select來知道什么時候可以從stdin讀?。?/p>

import sysfrom select import select

timeout = 10print "Enter something:",rlist, _, _ = select([sys.stdin], [], [], timeout)if rlist:
    s = sys.stdin.readline()
    print selse:
    print "No input. Moving on..."


查看完整回答
反對 回復 2019-08-08
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

如果您在Windows上工作,可以嘗試以下操作:

import sys, time, msvcrtdef readInput( caption, default, timeout = 5):
    start_time = time.time()
    sys.stdout.write('%s(%s):'%(caption, default));
    input = ''
    while True:
        if msvcrt.kbhit():
            chr = msvcrt.getche()
            if ord(chr) == 13: # enter_key
                break
            elif ord(chr) >= 32: #space_char
                input += chr        if len(input) == 0 and (time.time() - start_time) > timeout:
            break

    print ''  # needed to move to next line
    if len(input) > 0:
        return input    else:
        return default# and some examples of usageans = readInput('Please type a name', 'john') print 'The name is %s' % ans
ans = readInput('Please enter a number', 10 ) print 'The number is %s' % ans


查看完整回答
反對 回復 2019-08-08
  • 3 回答
  • 0 關注
  • 502 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號