Collecting Typing Data
| categories: Transcripts
Today we worked out how to collect data related to typed input.
The key is to use the raw_input(prompt) function. It takes a string argument and returns the string that the user typed.
We started with a trivial script that prompts and gets the response.
ans = raw_input('Say what? ')
Then we grew it to prompt the user for words chosen from the list of the 1000 most common words. We read the words and made them into a list with split. Then we repeatedly chose a random word from the list and asked the user to type it. We recorded the prompt and their response in the list answers.
import random
# load up a list of 1000 common words
words = file('1-1000.txt').read().split()
ntrials = 10
answers = []
for i in range(ntrials):
word = random.choice(words)
ans = raw_input('Please type ' + word + ': ')
answers.append((word, ans))
First we enhanced our script to give our user a break after a few prompts.
import random
# load up a list of 1000 common words
words = file('1-1000.txt').read().split()
ntrials = 10
answers = []
for i in range(ntrials):
word = random.choice(words)
ans = raw_input('Please type ' + word + ': ')
answers.append((word, ans))
if (i % 4 == 3): # give the user a break
go = raw_input('take a break, type y to continue: ')
Then we enhanced our script to time the user's response. Now we are recording the prompt, answer, and the time in seconds for the response.
import random
import time
# load up a list of 1000 common words
words = file('1-1000.txt').read().split()
ntrials = 10
answers = []
for i in range(ntrials):
word = random.choice(words)
tstart = time.time()
ans = raw_input('Please type ' + word + ': ')
tstop = time.time()
answers.append((word, ans, tstop - tstart))
if (i % 4 == 3):
go = raw_input('take a break, type y to continue: ')
Then we went a little crazy and jumbled the prompt. That sure increased the challenge.
import random
import time
# load up a list of 1000 common words
words = file('1-1000.txt').read().split()
ntrials = 10
answers = []
for i in range(ntrials):
word = random.choice(words)
prompt = list(word) # bust the string into list of single letters
random.shuffle(prompt) # shuffle the letters
prompt = ''.join(prompt) # rejoin the letters into a string
tstart = time.time()
ans = raw_input('Please type ' + prompt + ': ')
tstop = time.time()
answers.append((word, ans, tstop - tstart))
if (i % 4 == 3):
go = raw_input('take a break, type y to continue: ')
Finally, we added writing the data into a file as we went so we don't have to worry about losing it.
import random
import time
id = raw_input('Enter use id: ') # get some name for the file
outfile = file(id + '.txt', 'w') # create a file for this user's data
# load up a list of 1000 common words
words = file('1-1000.txt').read().split()
ntrials = 10
answers = []
for i in range(ntrials):
word = random.choice(words)
prompt = list(word)
random.shuffle(prompt)
prompt = ''.join(prompt)
tstart = time.time()
ans = raw_input('Please type ' + prompt + ': ')
tstop = time.time()
answers.append((word, ans, tstop - tstart))
print >>outfile, word, ans, tstop - tstart # write the data to the file
if (i % 4 == 3):
go = raw_input('take a break, type y to continue: ')
outfile.close() # close the file