My problem can be reduced to this: The participant presses a key, then a text appears saying "you pressed ..."
For that purpose, I created two routines. In routine 1, there is a text and keyboard (key_resp) component. In text component, it simply says "please press 1 or 2". The keyboard component has allowed keys 1 or 2.
In the second routine I have a code and text_2 component. The code is in "begin routine" tab with the code:
if key_resp.keys[0] == '1':
message = 'you pressed one'
else:
message = 'you pressed two'
In text_2 component, I have "$message" only. Now, when I run this, I get the error:
Name Error: name 'message' is not defined.
Then I added a code in routine 1 in "begin experiment" tab. The code is simply:
message = 'This message will not be displayed because of if/else in routine 2'
Now, this time the experiment actually starts, but it shows the unwanted message. How can I display the participant's keyboard response?
You must set your text_2
component text field to update "Every routine" rather than be constant. If that field is set to be constant, it is assumed that the value is available at the start of the experiment. So if it is not defined at that stage, you get an error (which you have diagnosed by providing a set value, defined at the start). You can safely delete that code now, as you are providing a value that gets updated on every run of the routine.
Please note that the community forum at https://discourse.psychopy.org is probably a better place to get support like this. StackOverflow is more oriented towards code-based questions and answers that will be of general interest to others, and that can be answered with a single question/answer format (whereas the forum allows for to-and-fro discussion to solve issues).