![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
Is there a way to skip on from the raw_input() or input() if nothing is entered in a certain ammount of time??
![]() So if a user has not entered anything in say 5 seconds, it will declare the variable blank, and move on! ![]()
__________________
while me is alive: make(life,simple) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
You'll have to use some type of programming that reads the keyboard input buffer and checks it for new key entries and times out if the buffer is still empty after X number of seconds.
__________________
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
Right, ok then
thanks.
__________________
while me is alive: make(life,simple) |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
I'm pretty sure you'll need Pygame for that. Once you get on MSN I'll probably help you with that.
|
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
import sys, select
def readWithTimeout(message, timeout):
""" Like raw_input, but you specify a timeout (in milliseconds). If that time
is reached, then this just returns a blank string. """
poller = select.poll()
poller.register(sys.stdin, select.POLLIN)
output = poller.poll(timeout)
if output:
return sys.stdin.readline()
else:
return ""
# Timeouts after 5 seconds.
name = readWithTimeout("Yeah, your name like: ", 5000) |
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Ran Cerulean's code and got this error ...
poller = select.poll() AttributeError: 'module' object has no attribute 'poll'
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Meh, select.poll is *nix only as well. Oh well.. here's the same example using select.select instead of select.poll (which, unless i'm grossly mistaken, is cross platform
). The reason I didn't use it to begin with is that select.poll is more readable.import sys, select
def readWithTimeout(message, timeout):
""" Like raw_input, but you specify a timeout (in seconds). If that time
is reached, then this just returns a blank string. """
output = select.select([sys.stdin], [], [], timeout)
if len(output[0]):
return sys.stdin.readline()
else:
return ""
# Timeouts after 5 seconds.
name = readWithTimeout("Yeah, your name like: ", 5) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|