Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fcntl
- import os
- import select
- import sys
- import time
- import termios
- import tty
- if not sys.stdin.isatty():
- raise RuntimeError('consoleio on non-tty')
- fd = sys.stdin.fileno()
- termstash = termios.tcgetattr(fd)
- tty.setraw(fd)
- flagstash = fcntl.fcntl(fd, fcntl.F_GETFL)
- fcntl.fcntl(fd, fcntl.F_SETFL, flagstash | os.O_NONBLOCK)
- score = 0
- buf = b''
- try:
- t0 = time.time()
- tend = t0 + 3
- sys.stdout.buffer.raw.write(b'Mash digits as fast as you can')
- while True:
- tnow = time.time()
- if tnow >= tend:
- break
- r, _, _ = select.select([sys.stdin.fileno()], [], [], tend - tnow)
- if r:
- ch = sys.stdin.buffer.raw.read(1)
- buf += ch
- score += sum(chr(c).isdigit() for c in ch)
- finally:
- termios.tcsetattr(fd, termios.TCSANOW, termstash)
- fcntl.fcntl(fd, fcntl.F_SETFL, flagstash)
- print('\nScore:', score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement