Feedback sound doesn't change or a tone is played

OS (e.g. Win10): Win 10
PsychoPy version: 2024.2.4
Standard Standalone? (y/n) If not then what?: Y
What are you trying to achieve?: Sound feedback for correct responses only in trials of Block type 1, and sound feedback for incorrect responses only in trials of Block type 2. The task randomizes the order of block types. When the task starts with Block type 1, the same sound feedback is played after each response regardless of correct or incorrect status. When it starts with block type 2, instead of the sound being played a tone is played that doesn’t stop and the trial doesn’t stop. Here is my code:

Begin experiment
soundfile = [“correctding.mp3” , “incorrectding.mp3”]
score = 500
previous_result = “”
Begin routine
playsound = None
#if blocktype is 1, reinforcement
if (blocktypeFlag == 1):
if key_Test.corr:
score += 3
msg = “Correct!”
playsound = soundfile[0]
scoreText.setText(f"Score: {score}")
scoreText.color = ‘green’
previous_result = “win”

else:
    msg = "Oops!"
    scoreText.setText(f"Score: {score}")
    scoreText.color = 'white'
    previous_result = "lose"

if block is punishment type

elif (blocktypeFlag == 0):
if key_Test.corr:
msg = “Correct!”
scoreText.setText(f"Score: {score}")
scoreText.color = ‘white’
previous_result = “win”

else:
    playsound = soundfile[1]
    msg = "Oops!"
    score -= 3
    scoreText.setText(f"Score: {score}")
    scoreText.color = 'red'
    previous_result = "lose"

In my routine I have first the code component, followed by the sound component and my two text components.
What did you try to make it work?: I included the “playsound = None” line at the start of the Begin Routine to reset the sound at the start of each trial to make sure that my playsound variable is not retaining its value from the previous trial, and also because I kept getting an “UnboundLocalError: local variable ‘playsound’ referenced before assignment” error message.
I have also tried to add a “playsound = None” when silence should occur, but it doesn’t help.

What specifically went wrong when you tried that?: I’m not getting any error messages, but the problem continues: when blocktype 1 is presented first, the same sound feedback is played every time regardless of response, and when blocktype 2 is presented first, a non-stopping tone is played and it doesn’t stop, at which point I need to close the task.

Thanks for any help!!

Hello @agarciapenagos

You never set the sound file when the answer is wrong. You could set the sound volume to zero for the wrong answers.

You can add print-statements to check whether or not the if-condition is evaluated in the way you expect, if you doubt that it is being evaluated correctly.

Best wishes Jens

Thank you so much!! I should have thought about it before! It is working well now!