# Word by word and response time at pavlovia

**URL:** <https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648>\
**Category:** Online experiments\
**Tags:** online, pavlovia\
**Created:** [October 25, 2024, 5:31pm UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648 "2024-10-25T17:31:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RolaWakid](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@RolaWakid](https://discourse.psychopy.org/u/RolaWakid)\
**Post date:** [October 25, 2024, 5:31pm UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/1 "2024-10-25T17:31:19Z")

</div>

URL of experiment:

Description of the problem:  
Hi,  
I have tried to upload my experiment to Pavlovia, but it does not work, I have to split a sentence into words, and measure time for some words, at this code : begin routine:  
words = sentence.split()  
i = 0  
frameCounter = 0 #it all happens within the same routine so make our own counter  
each frame:

# Assuming you have loaded the current trial from your conditions file

# Get all words from the columns like word\_1, word\_2, etc.

word\_columns = [col for col in thisTrial.keys() if col.startswith(‘word\_’)] # Dynamically fetch word columns  
words = [thisTrial[word] for word in word\_columns] # List of all words in the sentence

# Initialize the currentWordIndex

currentWordIndex = 0

# Initialize a list to store response times for each word

response\_times = [None] \* len(words) # Store response times for all words

# Main loop to update the word display based on key press

while currentWordIndex \< len(words): # Loop through all words  
# Display the current word and get the time  
text\_desplay.setText(words[currentWordIndex])  
text\_desplay.draw()  
win.flip()

```
start_time = core.getTime() # Start time when the word is shown

keys = event.waitKeys(keyList=['space', 'escape']) # Wait for key press

# If the spacebar is pressed, record response time for the current word
if 'space' in keys:
    response_time = core.getTime() - start_time # Calculate response time
    response_times[currentWordIndex] = response_time # Store the response time

    currentWordIndex += 1 # Move to the next word
    
if 'escape' in keys: # To allow exiting the experiment early
    core.quit()

```

# After the loop, retrieve response times for all words

# For example, you can save response times for target\_word, after\_word, last\_word

thisExp.addData(‘TargetWordRT’, response\_times[0]) # Assuming target is word\_1 for example  
thisExp.addData(‘AfterWordRT’, response\_times[1]) # Assuming after word is word\_2 for example  
thisExp.addData(‘LastWordRT’, response\_times[-1]) # Last word in the sentence  
thisExp.nextEntry() # Move to the next entry in the data file

it worked well offline but not online, I got this error: Unfortunately we encountered the following error:

- **ReferenceError: thisTrial is not defined**

Try to run the experiment again. If the error persists, contact the experiment designer.  
Thank you for your help!  
Ok

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [October 27, 2024, 12:54pm UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/2 "2024-10-27T12:54:55Z")

</div>

Hello

Your error description is difficult to read because of its formatting. Surround code by triple `, e.g.

```auto
print

```

instead of  
print

You surrounded your main loop code.

None of your code seems to be related to the error message. When do you use **thisTrial**? BTW, you cannot use a trial related variable, e.g. thisTrial.thisN, before the routine has started.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![RolaWakid](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@RolaWakid](https://discourse.psychopy.org/u/RolaWakid)\
**Post date:** [November 6, 2024, 9:38am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/3 "2024-11-06T09:38:24Z")

</div>

hi,  
can you help me detect the error, my experiment works offline but not online  
Thank you!  
Rola

‫בתאריך יום א׳, 27 באוק׳ 2024 ב-15:05 מאת ‪Jens Bölte via PsychoPy‬‏ \<‪[notifications@psychopy.discoursemail.com](mailto:notifications@psychopy.discoursemail.com)‬‏\>:‬

> **(attachments)**
>
> [asaf - Copy - 1.psyexp](https://discourse.psychopy.org/uploads/short-url/4OvNGYDaGDSkn97W8QM52l7zbgW.psyexp) (14.7 KB)  
> [sentences.xlsx.xlsx](https://discourse.psychopy.org/uploads/short-url/kUUP4S4tm8n00IKWqrIkX2pKsLp.xlsx) (8.92 KB)

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 6, 2024, 9:47am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/4 "2024-11-06T09:47:01Z")

</div>

Why do you have

```auto
    text_desplay.setText(words[currentWordIndex])
    text_desplay.draw()
    win.flip()

```

in Each Frame when you could have a text component in the same routine and simply use text\_display.text = words[currentWordIndex] ?

Avoid using win.flip() in Builder experiments. There is a flip at the end of each frame.

---

<div class="post-metadata">

**Author:** ![RolaWakid](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@RolaWakid](https://discourse.psychopy.org/u/RolaWakid)\
**Post date:** [November 6, 2024, 10:11am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/5 "2024-11-06T10:11:33Z")

</div>

I tried to replace my code with your suggestion, but it didn’t work offline or online.  
When I put the code in the same routine it doesn’t work offline, so I put it into a separate routine.  
may you explain more please?  
thank you!

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [November 6, 2024, 11:11am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/6 "2024-11-06T11:11:44Z")

</div>

Hello

@wakecarter has a self-paced reading example in [PsychoPy Online Demos](https://discourse.psychopy.org/t/psychopy-online-demos/22319). You might to use this as a start for a word by word display with saving RT for each word.

The attached example is a simplified self-paced reading experiment.

[SelfPacedShortVersion.psyexp](https://discourse.psychopy.org/uploads/short-url/ep2zxSNc8blnS6x9klAnqEEPQNj.psyexp) (33.4 KB)  
[warmUp.xlsx](https://discourse.psychopy.org/uploads/short-url/oyqmna3ioMUTFJigtLyMNoHCJG5.xlsx) (8.0 KB)

Both sources might give you some ideas how to implement a self-paced reading experiment.

You define trial as a column in your Excel-file and as routine. Avoid this, use unique names.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 6, 2024, 11:22am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/7 "2024-11-06T11:22:55Z")

</div>

While @JensBoelte was making a demo for you, I’ve also modified your code to something that words offline and should work online.

[asaf - Copy - 2.psyexp](https://discourse.psychopy.org/uploads/short-url/d7nwmCkVVcPOEbnwr1VSeWXCSMK.psyexp) (20.1 KB)

and a test data file

[test2\_asaf\_2024-11-06\_11h20.03.409.csv](https://discourse.psychopy.org/uploads/short-url/iFegqLYIu8Cl8zpgjD8eDjFtjzo.csv) (2.2 KB)

You had a words list being created two ways. I’ve kept your more complex 5 word columns but the result is that the word is appears three times because two of the cells are blank.

---

<div class="post-metadata">

**Author:** ![RolaWakid](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@RolaWakid](https://discourse.psychopy.org/u/RolaWakid)\
**Post date:** [November 7, 2024, 9:52am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/8 "2024-11-07T09:52:09Z")

</div>

I got this erro at pavlovia also: Unfortunately we encountered the following error:

- **ReferenceError: thisTrial is not defined**

Try to run the experiment again. If the error persists, contact the experiment designer.  
may this because my psychopy version is not same as yours?  
Thank you!

---

<div class="post-metadata">

**Author:** ![RolaWakid](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@RolaWakid](https://discourse.psychopy.org/u/RolaWakid)\
**Post date:** [November 7, 2024, 9:53am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/9 "2024-11-07T09:53:49Z")

</div>

Hello,  
the experiment didn’t work at my psychopy version so that I couldn’t sync to pavlovia. maybe I need another version ? mine is v.2021.1.4

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [November 7, 2024, 10:31am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/10 "2024-11-07T10:31:04Z")

</div>

Hello

No, this error

> [@RolaWakid](#):
>
> ReferenceError: thisTrial is not defined

is not related to the PsychoPy-version. But you might want to update to either 2024.1.4 or 2024.2.4. anyway.

The error tells you that you refer to a variable called **thisTrial** , however, there is no such variable. Usually, such variable refers to a loop-name.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 7, 2024, 11:03am UTC](https://discourse.psychopy.org/t/word-by-word-and-response-time-at-pavlovia/42648/11 "2024-11-07T11:03:06Z")

</div>

You have Python code

```auto
word_columns = [col for col in thisTrial.keys() if col.startswith('word_')] # Dynamically fetch word columns

```

This may work locally but thisTrial isn’t understood by Pavlovia.

Personally I would go back to words = sentence.split()
