Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] How to use items in a list as variables

edited September 2014 in OpenSesame

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

  • edited 3:12AM

    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.

    import numpy as np
    # Load the list from a file named `words.txt` that is placed in the file pool.
    # Change the source file as necessary. (But maybe don't place it in the
    # python site-packages folder!)
    word_list = np.loadtxt(exp.get_file('words.txt'), dtype=str)
    # Shuffle
    np.random.shuffle(word_list)
    # Start at word 1 (i.e. index 0)
    word_nr = 0
    

    Next, in the prepare phase of an inline_script at the start of a trial sequence, you simply retrieve the word at index word_nr, set it as an experimental variable, and increase the word_nr counter.

    # Retrieve the word at position `word_nr`
    word = word_list[word_nr]
    # Set it as an experimental variable
    exp.set('word', word)
    # Increase the word counter
    word_nr += 1
    

    That's it, basically. Now you can use [word] in sketchpads etc.

    Cheers!
    Sebastiaan

  • edited 3:12AM

    :D I cannot thank you enough! I appreciate this tremendously.

    Take care!

  • edited 3:12AM

    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?

  • edited 3:12AM

    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!

  • edited 3:12AM

    When I refer to the list in the second loop, it says that it is undefined

  • edited 3:12AM

    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?

  • edited 3:12AM

    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).

This discussion has been closed.