0

Im still new to python. Just wondering if there is an easier way to accomplish my task.

This is my current code

win = visual.Window([800,800],monitor="testmonitor", units="deg")

msg = visual.TextStim(win, text='hello this is a test',pos=[0,+1],color='white')
msg.draw()
win.flip()
core.wait(2)

msg = visual.TextStim(win, text='statement 1',pos=[0,+1],color='white')
msg.draw()
win.flip()
core.wait(2)

msg = visual.TextStim(win, text='statement 2',pos=[0,+1],color='white')
msg.draw()
win.flip()
core.wait(2)

msg = visual.TextStim(win, text='statement 3',pos=[0,+1],color='white')
msg.draw()
win.flip()
core.wait(2)

Is there a way I can pull statements from an excel file?

For example: I would have a loop going on and each round it would pull the next line in the file instead of having to copy the code and write a new statement each time?

1
  • xlrd for actual Excel files or the csv module for csv text files. You could also put all the strings in a list (or other container) and iterate over it. Commented Jul 3, 2016 at 21:42

1 Answer 1

2

D on't
R epeat
Y ourself

Your solution uses 4 blocks of codes of 4 lines each, the last 3 lines are the same, and the first is almost the same too!

You can avoid this by using a loop, as you guessed.

Look at what changes between each of your blocks, and stores it in a tuple (for example). Then, loop through this tuple and apply the block with the variable element.

texts = (
    'hello this is a test',
    'statement 1',
    'statement 2',
    'statement 3'
)

for text in texts:
    msg = visual.TextStim(win, text=text, pos=[0, +1], color='white')
    msg.draw()
    win.flip()
    core.wait(2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.