<jupyter_start><jupyter_text>Exercise 4: Debugging practice Practice 1Here's some broken code. When fixed, it should display wide and narrowrectangles, in random order, and play a bleep sound if you respond 'w'for wide or 'n' for narrow, and buzzing sound otherwise. It shouldalso output the correctness of the response and the RT to standardoutput (i.e., the python screen). The .wav files it can be downloaded[here](http://sapir.psych.wisc.edu/classMaterials/psych711/buzzBleep.zip))<jupyter_code>import time
import random
from psychopy import visual, core, event,sound
win = visual.Window([800,600],color="blak", units='pix')

rect = visual.Rect(win,fillColor="blue",size=size)
aspect = {'wide':[200,100], 'narrow':[100,200]}
validKeys = {'wide':'w', 'narrow':'n'}

bleep = sound.Sound('sounds/bleep.wav')
buzz = sound.Sound('sounds/buzz.wav')

for curIter in range(20)
	win.flip()
	core.wait(.5)
	curAspect  = random.choice(aspect.values())
	rect.setSize(aspect[curAspect])
	rect.draw()
	win.flip()
	timer = core.Timer()
	resp = event.waitKeys(keyList=validKeys.values())
	if resp==validKeys[aspect]:
		print 1, timer.getTime()
		bleep.play()
	else:
		print 0, timer.getTime()
		buzz.play()<jupyter_output><empty_output><jupyter_text>Practice 2Here's some more broken code. This program should prompt you with an expression category (Happy, Sad, etc.) and then show a face. If you categorize the face correctly, you should see a green CORRECT. Otherwise a red ERROR. The program should present you with 40 trials. Download the faces [here](http://sapir.psych.wisc.edu/classMaterials/psych711/faces.zip) and place them inside a `faces` folder in the same directory as the script below.<jupyter_code>import sys
from psychopy import visual, core, event

categories = {'Happy'='F', 'Angry'='W', 'Sad'='T'}
actors = ['001m', '001w', '002m', '002w', '003m', '003w', '004m', '004w', '005m', '005w']
suffix = '_90_60.jpg'
responseMapping = {'up':1,'down':0}
numTrials = 40


def randomButNot(l,toExclude):
	chosen = random.choice(l)
	while toExclude in chosen:
		chosen = random.choice(l)
	return chosen

def generateTrials(numTrials):
	trials=[]
	propMatch  = .6
	for i in range(numTrials):
		emotionPrompt = random.choice(categories.keys())
		shownActor = random.choice(actors)
		isMatch = int(random.random()<propMatch)

		if isMatch:
			shownCategory = emotionPrompt
			targetFaceImage = shownActor+categories[emotionPrompt]+suffix
		else:
			shownCategory = randomButNot(categories.keys(), emotionPrompt)
			targetFaceImage = shownActor+categories[shownCategory]+suffix

		trials.append({	'isMatch':isMatch,
						'emotionPrompt':emotionPrompt,
						'shownActor':shownActor,
						'shownCategory':shownCategory,
						'targetFaceImage':targetFaceImage
						})

		return trials

trials = generateTrials(numTrials)

win = visual.Window([800,600],color="black", units='pix')
prompt = visual.TextStim(win=win,text='',color="white",height=60)
correctFeedback = visual.TextStim(win=win,text='CORRECT',color="green",height=60)
incorrectFeedback = visual.TextStim(win=win,text='ERROR',color="red",height=60)
pic = visual.ImageStim(win=win, mask=None,interpolate=True)

for curTrial in trials:
	win.flip()
	core.wait(25)
	prompt.setText(curTrial['emotionPrompt']+'?')
	prompt.draw()
	win.flip()
	core.wait(.5)

	win.flip()
	core.wait(.1)
	pic.setImage('faces/'+curTrial['targetFaceImage'])
	pic.draw()
	win.flip()
	response = event.waitKeys(keyList=responseMapping.values())[0]
	if response==responseMapping[curTrial['isMatch']]:
		correctFeedback.draw()
	else:
		correctFeedback.draw()
	win.flip()
	core.wait(.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14462469067913114, 'fraction_numerical': 0.02502062139125653, 'mean_word_length': 5.8641509433962264, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '415289', 'license': 'MIT', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1228, 'n_words': 290, 'path': 'notebooks/Exercise4-debugging.ipynb', 'repo_name': 'lupyanlab/programming_for_psychologists', 'size': '3637'}