Conditional branching not working as it should

Hello,

I am using Psychopy3 on a MacBook (stnadalone).

I am trying to add a component to my experiment where a routine is only displayed to participants if they press a certain key. I know this is covered in conditional branching but it does not seem to be working and I’m not quite sure why.

What I have tried so far
I have used the following code:
if your_keyboard_component_name.keys.lower() != ‘v’:
continueRoutine = False
as suggested by @Michael in
conditional branching topic. So i added this to my routine at the ‘begin routine’ part, using the name of the keyboard on my previous routine.

it gives me the following error:
AttributeError: ‘list’ object has no attribute ‘lower’

So when I use the code without the “.lower()”, then it will skip the second routine regardless of what I press.

And this is me trying it on a simplified version of my experiment. after figuring out how to make it skip only under the condition I press the key ‘x’, then I will have to figure out how to use it on my experiment which is a bit more complicated (I think).

In the above message I tried the code in a new psychopy file, with no previous code used anywhere else in the file, my experiment has many components of code and loops, I’m worried that if I can’t get it to work on a simple file that I will not be able to do it on mine.

The part of my experiment concerning this is as follows:

after the instructions, participants will be presented with a routine consisting of a problem, they then type the solution on screen (RAT_code consists of code so the problem question will change on each loop- named trials- from a pool of questions), if they do not know the solution to the problem then the correct answer routine will appear for a short amount of time. Here is where I want to implement the conditional branching code. I want participants to only see the right answer if they do not know the solution.

A potential problem here could be the code within the routine named RAT_answer, participants can type anything and then have to press enter to continue to the next routine (see below). I was going to assign a letter ie ‘x’ or a number if it is easier to allow participants to say they do not know the answer. I don’t know if the fact that they can type anything and then press enter would affect the code I need to make the “correct answer” routine pop up only when participant’s dont know the solution, otherwise skip straight to insight_Q.


.

Here is what my correct_answer routine looks like

and trials_Study_RAT is just a loop looping it once but it’s part of the randomisation of what problem question shows up from pool of questions.

Any help would be appreciated,
Thank you!

Hi @minipsycho, the error is because the response type (i.e., list or string) depends on your keyboard settings. I think you have set store to first or last key. If so, just replace the code with the following:

if len(your_keyboard_component_name.keys):  # If I have received a response
    if your_keyboard_component_name.keys.lower() != 'v':  # If the response, which is now a string not list, is not a 'v'
        continueRoutine = False  # quit routine

Also, in the code component of your last image you need to replace your colon with an equals sign.

thanks for replying!
I still get the error message AttributeError: ‘list’ object has no attribute ‘lower’

without the lower part, it still skips it regardless of what I press.
I also have the set store to “all keys”.

You’re right about the colon sign on my last image, I’ve changed that thank you. Unfortunately the routine is still skipping regardless of the code I press

Ok, then try:

if len(your_keyboard_component_name.keys):  # If I have received a response and it is in the list
    if 'v' not in your_keyboard_component_name.keys:  # If the response 'v' is not in the list
        continueRoutine = False  # discontinue routine

that worked! thank you so much

thank you for your help @dvbridges
one more question: how would you then add this to the output to tell you that they have been shown this correct answer

i know the code line needed is this:
thisExp.addData(‘x’, y)

but i’m not sure what to make x or y

Yes, you would add this at the end of your routine, in the code component “End Routine” tab. You might want thisExp.addData('Response', your_keyboard_component_name.keys).

1 Like

hello, sorry to bother again.

I am doing a second version to this where a routine (RAT_problem) containing a problem is shown for 30 seconds, participant is asked to press space if they know the answer and then the next routine (RAT_answer) is shown where they can type the answer.

If they can’t answer the question and the 30 seconds is over i want the correct answer (routine=correct answer) to show.

Therefore the I need the follwng to happen:

  • RAT_problem: press space within 30 seconds or let time run out
    • if space is pressed
      • RAT_answer routine start
    • If RAT_problem routine ends as 30 seconds ran out
      • correct_answer routine to start

the keyboard component in RAT_problem is named key_resp_RATproblem

what I have tried so far:
I just tried using the code before to do this. in RAT_answer routine I added code in the begin routine section:

if len(key_resp_RATproblem.keys):
if ‘space’ not in key_resp_RATproblem.keys:
continueRoutine = False

in correct_answer routine I added code in the begin routine section:

if len(key_resp_RATproblem.keys):
if ‘space’ in key_resp_RATproblem.keys:
continueRoutine = False

What works:
if I press space, everything goes as planned, the RAT_answer routine continues and the correct_answer routine does not show up.

the problem is that when I don’t press space and just let the time run out on the routine (30 seconds) I get the following error:

if len(key_resp_RATproblem.keys):
TypeError: object of type ‘NoneType’ has no len()

what have I done wrong?

Hi, no problem. You can see in your code what is happening, if you compile the script into Coder. You should see this code:

# check responses
    if key_resp_RATproblem.keys in ['', [], None]:  # No response was made
        key_resp_RATproblem.keys=None

What is happening here, is that the keys attribute is being set to None (a NoneType) if no response was made. So, to avoid your error, try the following:

if key_resp_RATproblem.keys is not None:
    if len(key_resp_RATproblem.keys):
1 Like

thank you so much for replying so quick!

so i did the following:

at the begin routine of RAT_answer

if key_resp_RATproblem.keys is not None:
    if len(key_resp_RATproblem.keys):
        if 'space' not in key_resp_RATproblem.keys:
            continueRoutine = False

at the begin routine of correct_answer

if key_resp_RATproblem.keys is not None:
    if len(key_resp_RATproblem.keys):
        if 'space' in key_resp_RATproblem.keys:
            continueRoutine = False

this makes the correct_answer skip if space is pressed and show if no answer is given, which is great. Also, RAT_answer shows up when space is pressed which is also great.
However, RAT_answer still shows up when no answer is given.

did I type something wrong?

Right, so you want RAT_answer to be skipped for no response, or if space is not pressed? If so:

if key_resp_RATproblem.keys is not None:
    if len(key_resp_RATproblem.keys):
        if 'space' not in key_resp_RATproblem.keys:
            continueRoutine = False
elif key_resp_RATproblem.keys is None:
    continueRoutine = False

amazing, that works, thank you, you’re a star