Code for practice trial conditional loop not working

Hello there! We’re trying to set up a conditional loop for the practice segment of our experiment, but it fails to run.

What we’re trying to achieve:

  1. For each practice trial, the participant responds by selecting a slider rating (radio buttons 1, 2, 3, 4). For an incorrect response, a feedback screen displays (“Uh oh! Try again”), followed by the SAME practice item again.
  2. If an incorrect response is made 3 times in succession, the task ends, with a feedback screen (“No problem, try later.”).
  3. For a correct response, a feedback screen displays (“Good job!”), followed by the next practice item.

What we’ve tried so far: We’re still tackling goal #1 described above. Screenshot of the Practice Routine:

Note: We’re using 2 sliders, to get the answer options to display in two columns (A & B on the top row, C & D on the 2nd row).

To this end, we’ve built in a Code component in the Practice routine, in which the following lines of code have been inserted:-

(i) Begin Routine tab

n1=0
n2=0

(ii) End Routine tab

n1=0
n2=0
if slider1.getRating() and slider2.getRating() is not None:
n1=slider1.getRating()
n2=slider2.getRating()
if n1==2:
continueRoutine= False
else:
msg=“Wrong! Please check the answer”
else:
continueRoutine = True

What’s the error message? When we try to run the experiment, it crashes, with the following message:-

Alert 4205:Python Syntax Error in ‘End Routine’ tab. See ‘n1=slider1.getRating()’ on line number 4 of the ‘End Routine’ tab.

For further info see 4205: Probable syntax error detected in your Python code — PsychoPy v2021.2

Have we tried to find a solution on this forum? Yes. We’ve looked up numerous threads, and the most similar description to our problem seems to be (post1) an unanswered post as yet.

We would greatly appreciate advice and suggestions on how to make this work!

cLEE. :smiley:

1 Like

Could it be an indent error? If you use three of this character:
`
on this forum then whatever is inside will be formatted as code - meaning we can see what indentation you’re using. Better yet if you could share the .psyexp file then I can have a look at how it compiles, it could also be an issue there!

1 Like

If I understand your code correctly (and you do not need n1 and n2 anywhere else in the script) you could test directely for the value of slider1 instead of assigning it to a variable.

This does not solve your problem.

Cheers Jens

1 Like

@ TParsons: Hi! Thank you so much for responding to this post! I’m afraid that I had to be away from work for a week, and returned to find that my colleague had solved the problem. As it turned out, there were 2 errors in coding, one of which was an indentation/spacing error… apparently, assigning a few white spaces in the line of code specifying an “if” condition is required for Python syntax [i.e., " if slider1.getRating()==2: <SPACE SPACE SPACE>"]

So, you were right! :smile:

I have posted the corrected code below.

Cheers!

cLEE.

2 Likes

@ JensBoelte: Hi! :smile: Thank you so much for taking time to respond to my post, and for your suggestion. I had to be away from work for a week, and returned to find that my colleague had resolved the problem. There were 2 errors in coding, one of which was an indentation error, & the 2nd of which was that that the slider had been assigned to a variable. As you recommended, this was solved by directly assigning the slider values to variables.

I have posted the corrected code below.

Cheers!

cLEE.

1 Like

CORRECTED CODE:

"Begin Experiment" tab

right_ans=0
wrong_ans=0

"Begin Routine" tab

if slider1.getRating()==2:
right_ans=right_ans+1
resimg=“smiley.png”
msg=“Wow!”
continueRoutine=True
else:
resimg=“thumbsdown.png”
msg=“No problem. Try again!”
wrong_ans=wrong_ans+1

"End Routine" tab

if wrong_ans==2:
msg=“No problem. Thank you for participating!”
practice1.finished=True
continueRoutine=False
psychoJS.quit()
else:
msg=“Wow!”
resimg=“smiley.png”