If statement not working

Hello all,

I am trying to program a simple experiment on Psychopy Builder 1.90.3 for python2 on Mac Sierra 10.12.

In this experiment, subjects indicate whether they felt a change in temperature during a stimulus. I am trying to define a new variable called ‘response’ depending on their response and whether it was the correct response or not. I would like to add this variable to .csv output file.
In the ‘begin experiment’ tab, I defined my new variable, aka ‘response’.
In the ‘end routine’ tab, I am trying to define the ‘response’ variable in an if statement. This is where it does not work. It does not seem to go through the if statement.
I have spent hours reading online and trying different solutions, but I cannot get it to work.

Here is the code:

keys=answer.keys
#if there is a difference and they detect it, response type =1
if corrans == 2 and keys == 2:
    response=1
#if there is no difference and they report no difference, response =2
elif corrans == 1 and keys == 1:
    response=2
#if there is a difference and they report no difference, response =3
elif corrans == 2 and keys == 1:
    response = 3
#if there is no difference and they report a difference, response =4
elif corrans == 1 and keys == 2:
    response = 4
thisExp.addData('Response_type', response)

I would truly appreciate any suggestions.

Thanks a lot

Hi.

Firstly, please follow this advice so that we can correctly interpret the structure of your code:

Secondly, it is likely that the issue is that you are comparing to numeric values like 2 when you actually mean to compare to a string value like '2'. i.e. for the purposes of getting a response from the keyboard, the '2' key is just a letter or symbol like any other key. It isn’t a numeric value, and so needs to be in quotes.

Hi Michael,

Thank you for your help.

I edited the code so it follows the advice you sent. However, your second suggestion worked. It was because I was not using the key response as a string value. It works fine now.

Thanks again.