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:
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.
If an incorrect response is made 3 times in succession, the task ends, with a feedback screen (“No problem, try later.”).
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.
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!
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!
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.
@ 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>"]
@ JensBoelte: Hi! 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.
if wrong_ans==2:
msg=“No problem. Thank you for participating!”
practice1.finished=True
continueRoutine=False
psychoJS.quit()
else:
msg=“Wow!”
resimg=“smiley.png”