Scoring odd/even task using code component

Hi,

I am programming an experiment in which participants are asked to make a judgement about whether a digit (from 1-9) is odd or even. To do this, I created an array in the “begin experiment” tab with the digits from 1-9.

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Then, in the “begin routine” tab:

shuffle(arr)
POE_Number.setText(arr[1])

And to track the digit that is being presented to participants, I added a line to the “end routine” tab to print the value for each trial:

thisExp.addData (‘POE_Number’, arr[1])

Now, I am trying to write additional code to score whether or not the participant was correct in their judgement. I have been playing around with the position of this code, but right now have it in the same “end routine” tab as the line above. This is what I have done:

if POE_key_resp.keys == ‘q’ and POE_Number == ‘1’ or ‘3’ or ‘5’ or ‘7’ or ‘9’:
thisExp.addData (‘POE_Score’, ‘Correct’)

elif POE_key_resp.keys == ‘p’ and POE_Number == ‘2’ or ‘4’ or ‘6’ or ‘8’:
thisExp.addData (‘POE_Score’, ‘Correct’)

elif POE_key_resp.keys is None:
thisExp.addData (‘POE_Score’, ‘No_Answer’)

else:
thisExp.addData (‘POE_Score’, ‘Incorrect’)

When I do this, every answer is marked Correct regardless of my response. Would really appreciate any help or advice!

Hi @Kate_Checknita

You can design this experiment by using a condition file, then you do not need to write even a single line of code :slight_smile:

But, if you like to do it using those codes, you can write something like this:

# Begin Experiment
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#begin routine:
shuffle(arr)
trial_number= arr[1]
POE_Number.setText(str(trial_number))
thisExp.addData (‘POE_Number’, trial_number)

# End Routine
if POE_key_resp.keys == ‘q’ and POE_Number in [1,3,5,7,9]:
    trial_accuracy= 'correct'

elif POE_key_resp.keys == ‘p’ and POE_Number in [2,4,6,8]:
    trial_accuracy= 'incorrect'

elif POE_key_resp.keys is None: # not sure if this is correct
    trial_accuracy= 'no_response'

else:
    trial_accuracy= 'incorrect'

thisExp.addData (‘POE_Score’, trial_accuracy)

1 Like

Thank you so much!!

1 Like

Just note this small correction:

if POE_key_resp.keys == ‘q’ and POE_Number == [1, 3, 5, 7, 9]:

should be:

if POE_key_resp.keys == ‘q’ and POE_Number in [1, 3, 5, 7, 9]:

i.e. you aren’t comparing the single value to a list (to which it will never be equal), but checking if that value is in that list.

You can also check directly if a number is odd or even by dividing it by two and checking the remainder (using the % modulus operator):

if POE_key_resp.keys == ‘q’ and POE_Number % 2 == 1: # an odd number

or:

elif POE_key_resp.keys == ‘p’ and POE_Number % 2 == 0: # an even number
1 Like