Problem in coding for randomized stimuli

Hi everyone,
I’m making a task that has 5 different image stimuli: One at the top and the others down.
I want to make this task this way; the subject should match the correct image that matches the upper image using mouse. Also, I want to get the wrong responses in results with the names of the images that aren’t correct but I can’t code this. Can anyone help me?

Hello Sahar,
I created a simplistic experiment for you, I think most of it is self-explanatory, and I’ll gladly answer any questions you have about it.

In the trial routine, I created five image components. Four images have a constant path (red.png, blue.png etc…). The last image (shown_image) has a dynamic path with the variable shownStimuli.

When the routine starts, I randomly choose one image from a list. Then, I assign the selected image to the shown_image component:

# Begin Experiment:
import random

#Begin Routine:
pick = random.choice([image_black, image_green, image_red, image_blue])

showStimuliName =
shownStimuli = pick.image

I save a variable called showStimuliName to save a track on the current image being shown. I’ll, later on, compare it to the stimuli the participant clicked on:

#End Routine
clickedStimuli = mouse.clicked_name[0]

In the following routine, you can compare showStimuliName to clickedStimuli. If they are equal, the correct image was chosen. Otherwise, not:

# Text:
$'Correct Answer' if showStimuliName==clickedStimuli else 'Wrong Answer'

# Foreground color:
$'green' if showStimuliName==clickedStimuli else 'red'


Image (9.2 KB)

It should give you a solid foundation to start working with.

Let me know if you have any questions,

Thank you so much Chen!
Your code solved my problem.

1 Like

Can I ask just one more question?
What code should I use for image opacity if the mouse hovers over the image?
I mean 4 images are down of the page.
I tried the others code but it doesn’t work in this task.

There might be a more efficient way to achieve it, but the following works:

# Each Frame:
for image in [image_green, image_black, image_red, image_blue]:
    if image.contains(mouse):
        image.opacity = 0.5
        image.opacity = 1

Happy I could help :slight_smile: