tags:

views:

21

answers:

1

Hi, I am a beginner at using Python,

I am trying to request input from the user via stdin as a string, but record the time it takes for the user to enter the input, so that it may be played back later.

For example:

"It could take me 10 seconds to type this sentence"

and then if I played that sentence back it would take the same amount of time to redisplay it...

Any basic ideas on which modules to use for timing / input? For output, I am just going to store all the recorded values and times in a dictionary object and read from them. (Seems to be the best way...)

Any guidance or help would be appreciated. I am not a beginner programmer, but just very new to Python, so don't feel like you have to write anything out for me.

Thanks!

A: 

The input built-in function is fine for the input itself, and standard Python library module time probably the best way to measure time for your purposes. A simple function you can write easily puts them together:

import time

def timed_input(prompt):
    start = time.time()
    s = input(prompt)
    return s, time.time() - start

This function requires a prompt string as the only argument and returns a tuple with two items: the string the user typed, then a floating-point number with the seconds (and fraction thereof) the user took to type (plus some miniscule overhead for the time the system took to show the user the prompt, but that's really a tiny fraction of a second).

So, you call it as, e.g.:

s, thetime = timed_input('Type now: ')

then you do whatever you want with string s and float thetime (it's not clear to me how you plan to structure the dictionary you want to store them in, but that's a completely different question than the one I've been answering at length, of course!-).

Alex Martelli
This is perfect! Do you know of any built in modules that would make it possible to time the individual keystrokes to the command line?
jeffrey