psychopy.org | Reference | Downloads | Github

Online exp- "Int()" cannot work--Goal: Code correct as "1" and incorrect as "0"

  1. Exp background
    #mouse-click #picture

Participants’ task: use mouse to click 1 out of 4 pictures;
Exp’s task:
a. compare the selected picture with the target in the condition file;
b. generate a “score” column
–correct coded as 1,
–incorrect coded as 0.

  1. Offline–able to get “score” if being run in Builder
    The line works:
    “thisExp.addData(‘score’, int(correctAcc == imageTest1))”

int(target pic in condition file == pic shown)

Able to get the “score”:

  1. Online–Unable to get “score” if being run in Pavlovia
    The line does not work:
    “psychoJS.experiment.addData(‘score’, int(correctAcc == imageTest1))”

The problem lies in:
int(target pic in condition file == pic shown)

Unable to get the “score”:

Thank you for reading my question. Appreciate your answer and help.

One thing I’ve found is that Javascript seems less able to evaluate and assign at the same time. Perhaps you could edit your Python code to:

score = (correctAcc == imageTest1)
psychoJS.experiment.addData(‘score’, int(score))

or even

if correctAcc == imageTest1:
     score = 1
else:
     score = 0
psychoJS.experiment.addData(‘score’, score)

Best wishes,

Wakefield

1 Like

Hi Wakefield, much appreciate your patience. :slight_smile:

I’ve tried both ways as you suggested:

First way score = (correctAcc == imageTest1)
psychoJS.experiment.addData(‘score’, int(score))

This way works only when I delete “int ()”, the output in “score” column is TRUE or FALSE.
image

Adding “int()” will generate “NaN”
image

Second way
“if correctAcc == imageTest1:
score = 1
else:
score = 0
psychoJS.experiment.addData(‘score’, score)”

This code cannot be recognized by the coder (thus unable to generate “RT”, “clicked”, and “score”)

The error might be related to nested if statement;
So I tried the following ways
a. replacing the “if else statement” with the following form

*It does not work.

b. Wrting only “if- else” statement


*Seems to not work.

With no prior knowledge of syntax, I can only learn through trials and errors, and imitation, which leads to wrong syntax. Appreciate your patience.

Many thanks.
Ye

Hi Wakefiled, I succeeded!

This is my code–round 6!

I copied the right syntax here if anyone with similar problem needs help in the future

if image_1.contains(mouse_record):
    continueRoutine = False
    psychoJS.experiment.addData ('RT', t)
    psychoJS.experiment.addData('clicked', imageTest1)
    if correctAcc == imageTest1 :
     score = 1
    else :
     score = 0
    psychoJS.experiment.addData('score', score)
elif image_2.contains(mouse_record):
    continueRoutine = False
    psychoJS.experiment.addData ('RT', t)
    psychoJS.experiment.addData('clicked', imageTest2)
    if correctAcc == imageTest2 :
     score = 1
    else :
     score = 0
    psychoJS.experiment.addData('score', score)
elif image_3.contains(mouse_record):
    continueRoutine = False
    psychoJS.experiment.addData ('RT', t)
    psychoJS.experiment.addData('clicked', imageTest3)
    if correctAcc == imageTest3 :
     score = 1
    else :
     score = 0
    psychoJS.experiment.addData('score', score)
elif image_4.contains(mouse_record):
    continueRoutine = False
    psychoJS.experiment.addData ('RT', t)
    psychoJS.experiment.addData('clicked', imageTest4)
    if correctAcc == imageTest4 :
     score = 1
    else :
     score = 0
    psychoJS.experiment.addData('score', score)

And IT WORKED!

Thank you so much for your help~! Stay well.
Ye

1 Like

Excellent.

Can you point to the aspect of my Python syntax that didn’t translate to working PsychoJS?

Best wishes,

Wakefield

Thank you. Yes.

1. This python code cannot run, due to the format of the single quotation mark on “score”.
I copied and pasted yours, and the single quotation mark did not turn into purple.

Deleting the previous one, and retyping a new one will work.

2. This python code didn’t run because of the position of “if” and “else”.

Making the “if” “else” statement parallel works.

3. So, I changed the location of the inserted “if” and “else” in the previous wrong syntax, and then it worked.

Before:

After:

Hope it answered your question. Thanks for your help.

1 Like