[solved] How to use items in a list as variables
Hello! I am a beginner with python and computer programming as a whole. I am designing a simple recognition task experiment in which words from a list are presented randomly on the screen and the subject must determine if he or she has studied these words.
I will have:
a study list of 50 words that a subject studies.
a test list of 100 words, 50 of them being from the study list
this is the code I have for python:
import numpy as np
words=np.loadtxt('C:\Python27\Lib\site-packages\mfwords.txt', dtype = 'string')
#use location of list of words form a text file of 600 or so available words
np.random.shuffle(words)
Basically, I want to run this section of code so that the words variable is randomized every time. Then I want to place each item in the list words[0], words[1], words[2].....words[49] in the variable column in the block sequence where I define variables that can later be pulled up on the sketchpad. I have little experience with coding and do not know how to import this into OpenSesame. I do not want to just type the words, because there are 600 in the pool and they need to be different for every subject.
I tried pasting this on a python inline script and placing words[0:50] under the word column in my variable slot, and text [word] in the sketchpad to pull up each of the 50 words from index 0 to 49. The code could not execute.
Is there a way to achieve this? I am totally lost
Comments
Hi,
So, if I understand correctly, you basically want to read in a list of words, shuffle it, and present the words one by one. That's it, right?
You've already got the first step covered: Loading and shuffling the list. You should do this in the prepare phase of an
inline_script
that is executed before you start looping through the list.Next, in the prepare phase of an
inline_script
at the start of a trial sequence, you simply retrieve the word at indexword_nr
, set it as an experimental variable, and increase theword_nr
counter.That's it, basically. Now you can use
[word]
insketchpad
s etc.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
I cannot thank you enough! I appreciate this tremendously.
Take care!
I was able to load the words and present them on a screen successfully! I do have one more question: How can I store those presented words in the first loop as a variable I can re-present in a second loop? For instance, if you studied cat, dog and mouse in the first phase, I would need to represent cat dog and mouse along with rabbit, bird, and fox in the second phase. With the above code it seems I redraw from the original pool of random words when I need the already studied words to reappear for testing later. I need to access them specifically so I can show them again. Any thoughts?
The words will still be stored in the same list, so you can simply redraw them from that list. The list was only shuffled once, so their order will still be the same the second time around.
Good luck!
When I refer to the list in the second loop, it says that it is undefined
I attempted to make the word_list global, but still it does not seem to recognize the python variable later on. Is there anyway to save the presented words to a new text file?
Closed, as a new discussion has been opened for this question (#1123).
Check out SigmundAI.eu for our OpenSesame AI assistant!