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 5:33PM

    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 5:33PM

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

    Take care!

  • edited 5:33PM

    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 5:33PM

    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 5:33PM

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

  • edited 5:33PM

    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 5:33PM

    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.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games