How to output data to file?
<p>So i am trying to write a Stroop experiment from scratch. 
Ideally this is how i could like the experiment to be set up:</p>

<ol>
<li>Enter participant information</li>
<li>Instruction pages (click x to continue)</li>
<li>Instruction page 2 (click x to continue)</li>
<li>Experiment Start</li>
<li>Break between trial</li>
<li>Experiment trial 2</li>
<li>End</li>
</ol>

<p>(there will be more than 2 trials but for testing just 2 will be used)</p>

<p>I'm having difficulty writing the data to a text file. The second trial records perfectly with the different values per each loop. However the first trial shows up as duplicates and each trial has the same values in the text file.</p>

<p>In addition, i can't figure out how to write the data from the pop-up into my text file. (ie. subject name, age, id)</p>

<p>Also is there a way I can input the file name each time? Without changing code? -perhaps like a popup to choose the path and file name?</p>

<p>Thank you!</p>

<pre><code>from psychopy import visual, core
import random
import time
import datetime
import sys
from psychopy import gui
from psychopy import event



#Write to file, need to figure out how to choose file name in each instance
file = open ("Test Output.txt", 'w')


#Pop up subject information - need to figure out how to output this data
myDlg = gui.Dlg(title="TEST TEXT BOX")
myDlg.addText('Subject info')
myDlg.addField('Name:')
myDlg.addField('Age:', )
myDlg.addText('Experiment Info')
myDlg.addField('Subject ID', "#" )
myDlg.addField('Group:', choices=["Test", "Control"])
ok_data = myDlg.show()
if myDlg.OK:
    print(ok_data)
else:
    print('user cancelled')

#opens up window w/ text, 
win = visual.Window([800,800],monitor="testmonitor", units="deg")
msg = visual.TextStim(win, text="Hello")
msg.draw()
win.flip()
event.waitKeys(maxWait=10, keyList=None, timeStamped=False) #page remains until keyboard input, or max of 10 seconds

#with keyboard input, second screen will come up
msg = visual.TextStim(win, text="Instructions 1")
msg.draw()
win.flip()
event.waitKeys(maxWait=10, keyList=None, timeStamped=False)

#3rd screen will pop up with keyboard input
msg = visual.TextStim(win, text="Trial 1")
msg.draw()
win.flip()
event.waitKeys(maxWait=10, keyList=None, timeStamped=False)


#Trial starts,
for frameN in range(5):
    MyColor = random.choice(['red','blue','green','yellow'])
    Phrase = random.choice(["Red","Green", "Blue", "Yellow"])
    time = str(datetime.datetime.now())
    key = str(event.getKeys(keyList=['1','2','3','4','5'], ))
    pause = random.randint(1200,2200)/1000.0
    length = str(pause)
    msg = visual.TextStim(win, text=Phrase,pos=[0,+1],color=MyColor)
    msg.draw()
    win.flip()
    core.wait(pause)


msg = visual.TextStim(win, text="Break between trial")
msg.draw()
win.flip()
event.waitKeys(maxWait=10, keyList=None, timeStamped=False)

#trial 2
for frameN in range(5):
    MyColor2 = random.choice(['red','blue','green','yellow'])
    Phrase2 = random.choice(["Red","Green", "Blue", "Yellow"])
    time2 = str(datetime.datetime.now())
    key2 = str(event.getKeys(keyList=['1','2','3','4','5'], ))
    pause2 = random.randint(1200,2200)/1000.0
    length2 = str(pause2)
    msg = visual.TextStim(win, text=Phrase2,pos=[0,+1],color=MyColor2)
    msg.draw()
    win.flip()
    core.wait(pause2)

#specifying which data will be recorded into the file
    data = "Stimuli:"+ MyColor + ',' + Phrase + ','+  time + ',' + key + ',' + length + MyColor2 + ',' + Phrase2 + ','+  time2 + ',' + key2 + ',' + length2

    file.write(data  + '\n')









#Jessica's Code.
</code></pre>