OS (e.g. Win10): Win10 PsychoPy version (e.g. 1.84.x): 3.0
**Standard Standalone? (y/n) y
**What are you trying to achieve?:
I am trying to port an existing PsychoPy experiment online and am having issues iterating through a dictionary so it runs online. Essentially I am creating a dict that adds a word and associated value (e.g., “cat” : 1) after each trial. I want Ps to recall as many words as they can (free recall) and then reference their typed responses to the dictionary that adds up the values.
**What did you try to make it work?:
#Begin Experiment
var word_rating = {}; # JS code to create a dictionary called word_rating
#End Routine
word_rating[word1] = value1; # add word and value from spreadsheet
# Typed Response Routine
Take typed responses and iterate through loop
response_list=[typed_response.text] # save typed responses to array
str1= response_list.join("")
str2=str1.split{"")
thisExp.addData("response1",str1)
thisExp.addData("response2",str2)
total = 0
for response in str2:
if response in word_rating.keys():
total = total + word_rating[response]
In python the above works fine.
**What specifically went wrong when you tried that?:
The experiment returns an error that it cannot find the var word_rating.
Any help on how to fix this would be greatly appreciated.
Hi @jgeller112, in Builder could you try placing var word_rating = {}; in the “Before Experiment” instead of the “Begin Routine” tab of your Code Component properties window? x
My apologies. I pushed the latest experiment with the newest version of PsychoPy. I declared the word_rating variable in the Begin_Experiment component with the var and it works. It does not work when placed in Before_Experiment component.
My main issue is that the I am not able to loop through the dictionary and reference it to the typed responses. I get an error about the variable value not being referenced in the JS code.
It is almost there! However, it does not seem to be adding up the values correctly.
Does JS store the letter strings typed out differently than Python? In Python the strings are stored as one letter string which is why I wrote some code to split them up and reference them to the dict.
Each word is associated with a certain value and typing them all should give me 30, but is is giving me 10. This makes me think JS is parsing the typed responses differently than Python?