pythonloopspsychopypredefined-variables

Python - Using a list of predefined variables in a loop (psychopy)


I am trying to write a piece of code that requires 200 text stimuli to be viewed for 1 second one after the other but I cant get my code to work.

Each stimulus is predefined as a variable:

redkey = visual.textStim(win, "key", (1.0, -1.0, -1.0)
bluekey = visual.textStim(win, "key", (-1.0, -1.0, 1.0)

I have a list 200 items long like so:

x = ['redkey', 'bluekey', 'bluekey', 'redkey'...]

I am trying to write a for loop which cycles through the list and displays each variable for a second (roughly) but I can't get it to work.

win = visual.Window([1024, 768], fullscr = false, allowGUI=false, color = (0.0, 0.0, 0.0))

for item in x:
    item.draw()
    win.flip()
    core.wait(1.0)

When I do this I get the error: AttributeError: 'str' object has no attribute 'draw'.

I tried playing around with vars() and eval() but I can't quite figure out how to do it. Is it even possible to do this?


Solution

  • I'm guessing that you should change:

    x = ['redkey', 'bluekey', ...]
    

    to

    x = [redkey, bluekey, ...]
    

    As it is, each item in 'x' is a string, rather than the class object that's required.