Using past trial text response to display a specific question in current trial

OS(e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.2.10
Standard Standalone? (y/n) If not then what?: y

What are you trying to achieve?: dummyrun2.psyexp (21.7 KB)
I am attempting to use the new beta textbox feature for a questionnaire, where the response to the second question determines what the third question to ask will be. My issue has two pieces:

(1) I am trying to figure out the best way to use the new textbox feature so that it is an empty box to start (currently you can not leave the “Text” field blank even when checking the textbox to “Editable”); (2) I would like to set up my code component to check the second question’s response and use the answer to determine what the third question will be. For the sake of ease (at least I think it is for ease), I am currently making each question a separate routine so that the code component for my third routine can read what occurred in the second routine.

What did you try to make it work?:
For part (1), I have simply entered “( )” in the “Text” field of the textbox component. This creates a nuisance in that the participant will need to delete this before entering their response. I am in the dark on a way around this.

For part (2), I have tried several variations of the following code component, which I thought would grab the answer to the second question (possibly as a string?) and match it to the conditions of the code component to display the proper question for the third question. Here is the general code outline I have been attempting:

the code component is currently placed in the top position of routine 3
Begin Experiment Tab:

#name of question variable for Question 3
Question_3text = " "

Begin Routine Tab:

#determining which question or statement to show after Question 2
if Response_2.text =="LSD":
    Question_3text = "enter dose in number of micrograms"
elif Response_2.text =="psilocybin":
    Question_3text = "enter dose in number of grams"
else:
    Question_3text = "sorry that is not a valid substance"

the reason I am currently attempting to use “Response_2.text” is because this is the name given to the output for Response_2 when I check the data output file.

What specifically went wrong when you tried that?:
The third question continues to display “sorry that is not a valid substance” even if I enter “LSD” or “psilocybin” in the second question’s textbox. In one sense, I at least know that the if statements are being read because of the third question. However this still leaves me wondering what I actually need to label “Response_2.text” in the code so that it pulls the participant’s question 2 response and uses that to determine what to display for the third question.

I’m an not very sure about the code writing maybe there is an error there. But, what if instead of placing the code component in the third routine you place it in the second routine and place this:

#determining which question or statement to show after Question 2
if Response_2.text ==“LSD”:
    Question_3text = “enter dose in number of micrograms”
elif Response_2.text ==“psilocybin”:
    Question_3text = “enter dose in number of grams”
else:
    Question_3text = “sorry that is not a valid substance”

in the end routine tab instead of the begin routine tab?

tandy

Thank you for your advice on code placement Tandy. I just gave it a shot moving the component to the end of the second routine. Intuitively I also thought this might work once you mentioned it, but to no avail.

I will be reviewing the source code web pages for how PsychoPy handles data.trial and visual.textbox2.textbox2 until further replies are made. Perhaps there is something in there that will tell me more about how a participant’s response in a textbox is logged and likewise how to access it.

Try some (temporary) debugging checks before your code:

print(Response_2.text)
print(type(Response_2.text))

Sometimes two variables only look like they are the same – programming languages are fussy about types and such, and possible invisible characters, like new lines.

Thank you Michael, just performed the debugging technique and did find something interesting. In the Experiment Runner the two printouts associated with the debugging commands are:

default text
<class 'str'>

Rather than updating the text to one of the questions, it remains as “default”, which in my case is just “( )”. Any idea on how to write this properly so that “default text” becomes the proper question? Thanks in advance!

So it looks like the code is simply running at the wrong time, since it shows “default text” rather than the typed value that is apparently being stored correctly in your data file.

What tab in the code component is this code placed in? It needs to run after the response is completed. At a guess, this would be in the “end routine” tab, if on the routine in which the response is made. But @tandy already figured that out.

I wish I could say that the code placement would fix the issue. After attempting placing the code at the end of the second routine, it still does not update and show the proper question.

I have also placed the code component at multiple different places within the routine and it didn’t work.