| Reference | Downloads | Github

event.waitKeys problem

So in this scenario, after a stimulus display on every trial, participants are prompted to give a response. Participants are only given 2 seconds (due to the prerequisite of the experiment) to give a response. In the below given block of codes, it works when a participant gives a response within the span of 2 seconds. However, if a participant did not provide a response during this time frame, the variable ‘Trespraw’ will contain no data at all. When this happen, it will generate a TypeError: ‘NoneType’ object has no attribute ‘getitem error when the script reaches the if statement sections.

Tresp is basically a variable to store the string data to be inputted in the csv file at the end.

Is there actually a way to get Trespraw (event.waitKeys to be specific) to generate something rather than provide a NoneType item when participants weren’t able to provide a response in time? Or could anybody think of a work around solution to this?

Trespraw = event.waitKeys(maxWait= 2.0, keyList = ['up', 'down','left', 'right'], timeStamped=True)

Tresp = 'Empty'
if Trespraw[0][0] == 'up':
    Tresp = 'Up'
if Trespraw[0][0] == 'down':
    Tresp = 'Down'
if Trespraw[0][0] == 'left':
    Tresp = 'Left'
if Trespraw[0][0] == 'right':
    Tresp = 'Right'

The easiest workaround would probably be to check if Trespraw is anything other than None before you continue:

if Trespraw and Trespraw[0][0] == 'up':
    Tresp = 'Too slow'

This performs the Trespraw[0][0] operation only if Trespraw exists. The else statement makes sure you have a value in Tresp anyways.

/E: I also just wanted to edit to say that waitKeys returning None is the correct behaviour: You waited for keys, and none were pressed, so the result shouldn’t contain anything. So rather than a “workaround”, this is a correct way to do it :stuck_out_tongue:


Jan is right, you could add another condition checking if Trespaw is None, and replacing it with a blank string, and put it before your other conditions.

if Trespraw == None:
    Tresp = ""

The problem is when you do this:


You are asking a ‘None’ object for the first item in its first item, and since None objects by design don’t contain items, you’re getting an error. So you have to make sure it’s not None before you ever try to index it with [0].

1 Like

Please note that checking for None should usually be done via is, not ==:

if Trespraw is None:


Since in the original code example, all @angjw.aaron does is convert the first character to uppercase, the entire thing could be simplified like:

if Trespraw:
   Tresp = Trespraw[0][0].title()
    Tresp = 'Too slow'

I personally believe it should return an empty list in that case, but this change would break backward compatibility.

1 Like