PennController for IBEX › Forums › Support › Using if statements during a trial
- This topic has 10 replies, 4 voices, and was last updated 2 years, 5 months ago by grusha.prasad.
-
AuthorPosts
-
June 3, 2020 at 11:43 am #5570mrhelfrichParticipant
Hello,
I have a kind of convoluted question.
My overall goal is to have an experiment where participants read a test item taken from a .csv file. The .csv file also has a column with an comprehension question for some test items, and therefore if the test item they just read has a comprehension question for it, I want them to be shown that instead of the next test item in the .csv file being displayed.
My plan on how to accomplish this was to use an if statement associated with another column in the csv file. The CSV file has a column that holds either a Y or N for whether or not the test item has a question attached. So after the test item is displayed, if no question was going to be asked, a button would appear telling them to proceed to the next trial, but if there was a test item, a different button would appear telling them they would be taken to a comprehension question. My issue is that I am getting syntax problems with the if statement so I cannot test anything.
I’ve simplified the code as much as possible to just isolate the problem to post to the forum:
PennController.ResetPrefix(null); AddTable( "IfTable" , // for the sake of forum posting, actual experiment uses an embedded .csv file "TrialNum,FullText,CompYN,CompQ,CompR,CompTF\n"+ "1,Trial 1 does not have a comprehension question.,N,,,\n"+ "2,Trial 2 does have a comprehension question.,Y,Placeholder question for Trial 2 click button to continue no response needed correct answer will be 1 or True.,2,F\n"+ "3,Trial 3 does have a comprehension question.,Y,Placeholder question for Trial 3 click button to continue no response needed correct answer will be 2 or False.,2,F\n"+ "4,Trial 4 does not have a comprehension question.,N,,,\n"+ "5,Trial 5 does have a comprehension question.,Y,Placeholder question for Trial 5 click button to continue no response needed correct answer will be 1 or True.,1,T\n" ); Template( "IfTable" , row => newTrial( "experiment" , newText ("FullText", row.FullText), newButton("FTButton1","Proceed to next Trial"), newButton("FTButton2","Proceed to Comprehension Question"), newText("CompQ",row.CompQ), newButton("CQButton","Placeholder, just click to answer"), getText("FullText") .print(), if (row.CompYN==Y){ getButton("FTButton2") .print() .wait() .remove("FTButton2","FullText"), getText("CompQ") .print(), getButton("CQButton") .print() .wait() .remove("CompQ","CQButton") } else if (row.CompYN==N){ getButton("FTButton1") .print() .wait() .remove("FTButton1","FullText") } ) //closes newTrial ) //closes Template
I am not a programmer so I could be making any number of mistakes, the only solution I could come up with to have follow-up questions appear after certain items was an if statement.
Thank you for your time,
Max- This topic was modified 4 years, 3 months ago by mrhelfrich.
June 3, 2020 at 12:02 pm #5572JeremyKeymasterHello Max,
TLDR: this is the (or rather, one) correct syntax
newTrial( "experiment" , newText ("FullText", row.FullText), newButton("FTButton1","Proceed to next Trial"), newButton("FTButton2","Proceed to Comprehension Question"), newText("CompQ",row.CompQ), newButton("CQButton","Placeholder, just click to answer"), getText("FullText").print(), ( row.CompYN==Y ? getButton("FTButton2") .print() .wait() .remove("FTButton2","FullText"), getText("CompQ") .print(), getButton("CQButton") .print() .wait() .remove("CompQ","CQButton") : getButton("FTButton1") .print() .wait() .remove("FTButton1","FullText") ) ) //closes newTrial
You cannot inject if statements this way into a PennController trial, because the sequence of commands that you pass to newTrial really are arguments that you pass to a function, and javascript does not allow you to insert if statements in this kind of environment. What you can do, however, is use the so-called inline ternary conditional operator, which uses this syntax: ( test ? success : failure )
Note that the condition will be evaluated at the very beginning of your experiment, which is fine in your case because you are testing the value of a column from your table. If you wanted to test a value that is manipulated upon runtime however, say some input from your participant, you would need to use a PennController test command, to ensure that the test is performed only after input.
Jeremy
June 4, 2020 at 9:58 am #5579mrhelfrichParticipantThank you for the quick reply, unfortunately the code you posted is throwing syntax errors when I copy it over. I am not great at understanding the error messages but it seems like it by default after the test ? error checker only allows one get command. I added parentheses to envelop all of the commands that I wanted to run before the : and this gets rid of the syntax errors but when I run the code it doesn’t execute properly (I also had to change
row.CompYN==Y ?
torow.CompYN=="Y" ?
since it was treating it as an expression. I am not certain but it appears that putting parentheses around the multiple commands only stops the syntax errors and the conditional operator you showed me will only run the last command listed before the colon. I changed the text displayed on the buttons to make it easier to see but it looks like the test itself functions properly but it doesn’t execute all of the code whenrow.CompYN=="Y"
is true.Here’s the code I have been running that at doesn’t give syntax errors but doesn’t run as expected.
PennController.ResetPrefix(null); // for the sake of forum posting AddTable( "IfTable" , "TrialNum,FullText,CompYN,CompQ,CompR,CompTF\n"+ "1,Trial 1 does not have a comprehension question.,N,,,\n"+ "2,Trial 2 does have a comprehension question.,Y,Placeholder question for Trial 2 click button to continue no response needed correct answer will be 1 or True.,2,F\n"+ "3,Trial 3 does have a comprehension question.,Y,Placeholder question for Trial 3 click button to continue no response needed correct answer will be 2 or False.,2,F\n"+ "4,Trial 4 does not have a comprehension question.,N,,,\n"+ "5,Trial 5 does have a comprehension question.,Y,Placeholder question for Trial 5 click button to continue no response needed correct answer will be 1 or True.,1,T\n" ); Template( "IfTable" , row => newTrial( "experiment" , newText ("FullText", row.FullText), newButton("FTButton1","Proceed to next Trial"), newButton("FTButton2","SHOULD DISPLAY IMMEDIATELY AFTER TRIAL 1 AND 4"), newText("CompQ",row.CompQ), newButton("CQButton","SHOULD DISPLAY WITH COMPQ TEXT ABOVE"), getText("FullText").print(), (row.CompYN=="Y" ? // functions like an if statement based on the specified test, if row.CompYN==Y, then the following code is executed, if not, run code after the colon ( // parentheses needed to include multiple commands as part of the 'success' branch of the above test getButton("FTButton2") .print() .wait() .remove("FTButton2","FullText"), getText("CompQ") .print(), getButton("CQButton") .print() .wait() .remove("CompQ","CQButton") ): // close parentheses and then colon marks the change to the 'failure' branch of the test getButton("FTButton1") .print() .wait() .remove("FTButton1","FullText") ) // closes the 'test' ) // closes newTrial ) // closes Template
Thanks again for all of your help,
MaxJune 4, 2020 at 10:06 am #5580JeremyKeymasterHi Max,
I’m sorry, I went too fast when posting my answer, here is a revised version of the code:
newTrial( "experiment" , newText ("FullText", row.FullText), newButton("FTButton1","Proceed to next Trial"), newButton("FTButton2","Proceed to Comprehension Question"), newText("CompQ",row.CompQ), newButton("CQButton","Placeholder, just click to answer"), getText("FullText").print(), ( row.CompYN=="Y" ? [ getButton("FTButton2") .print() .wait() .remove("FTButton2","FullText"), getText("CompQ") .print(), getButton("CQButton") .print() .wait() .remove("CompQ","CQButton") ] : [ getButton("FTButton1") .print() .wait() .remove("FTButton1","FullText") ] ) ) //closes newTrial
Besides the lack of double-quotes around Y, the problem was that each comma-separated block of commands represents an object, but the ( test ? success : failure ) structure allows only one object in success and failure. There technically were three objects in the success part of the original code, so the first two were ignored. The solution I just proposed is to package the three objects into a single array: because newTrial treats commands packaged in an array the same way as direct series of commands, you still get the intended result.
Jeremy
June 4, 2020 at 11:27 am #5584mrhelfrichParticipantOkay, that seems to have solved the problem. I have one minor question now that the code is executing properly.
The way my code is currently set-up, it draws the text I want and immediately after it draws the button to continue, and depending on the what you helped me with that button will be one of two different buttons. When the test fails, button drawn ends the loop and the next trial occurs and all of the old text gets removed, but when the test succeeds, clicking the button to go to the comprehension question draws the comprehension question and then a different button that will end the loop. I was previously hoping that by specifying what items I wanted removed inside the parentheses of the .remove() line would make it remove those items but now that the code is executing properly that doesn’t do anything and the .remove() command just removes whatever it is attached to. When the test succeeds andgetButton("FTButton2")
appears, upon clicking it I want both the button itself to be removed as well as the previous commandgetText("FullText")
to be removed. Is there a command to remove previously drawn text sort of like what happens when a trial completes and a new loop starts?Thanks again for all of the help,
MaxJune 4, 2020 at 11:55 am #5586mrhelfrichParticipantUnable to edit my reply but I figured out how to resolve the problem, I just needed to call
getText("FullText")
again and then use .remove().June 4, 2020 at 11:57 am #5587JeremyKeymasterHi Max,
As you say, the remove commands applies to the element it attaches to, so just attach it to the elements you want to remove:
getButton("FTButton2") .print() .wait() .remove(), getText("FullText").remove(), getText("CompQ") .print(), getButton("CQButton") .print() .wait() .remove(), getText("CompQ").remove()
Jeremy
June 25, 2020 at 8:15 am #5716mschrumpfParticipantHello everybody,
I am currently working on a very similar experiment: a self-paced reading task where some but not all of the items have a comprehension question.
I tried to apply your code from earlier in this thread, Jeremy, as stated below. My implementation has completely taken out the skript however, as I now only get the “There must be some items in the running order” error message when I try to run it.
This is my code:Template( "MyTable.csv" , row => newTrial( "experiment" , dashed("sentence", row.Satz) , newButton("Fertig") .print() .wait() , newTimer(200) .start() .wait() , newText("Frage", row.Frage) , newText("Links", row.Links) , newText("Rechts", row.Rechts) , newCanvas("Canvas", 450,200) , newSelector("Selector") , newTimer("200", 200) .start() .wait() , ( row.Antwort=="1" ? [ getText("Frage") .print(), getText("Links") .print(), getText("Rechts", row.Rechts) .print(), getCanvas("Canvas") .add( 0 , 0 , getText("Links") ) .add( 250 , 0 , getText("Rechts") ) .print(), getSelector("Selector") .add ( getText("Links"), getText("Rechts")) .keys( "F" , "J") .log() .once() .wait() ] : [ getText("Frage") .print() //Placeholder code. The final skript should just "do nothing" here ] ) ) .log("Item" , row.Item ) .log( "Konsistent" , row.Konsistent ) .log( "Geschlecht" , row.Geschlecht ) .log( "Krit" , row.Krit ) .log( "Anzahl" , row.Anzahl ) .log( "Antwort" , row.Antwort ) .log( "Satz" , row.Satz ) .log( "Frage" , row.Frage ) .log( "Links" , row.Links ) .log( "Rechts" , row.Rechts ) )
Since the error message is giving me nothing to work with, I would be very grateful for any hints as where I have gone wrong.
Thanks in advance and best regardsJune 25, 2020 at 10:25 am #5719JeremyKeymasterHello,
Your problem must be coming from another part of your script, because I just tested what you posted and it works as expected, I don’t get any error message
Did you make sure you properly defined dashed?
Jeremy
April 6, 2022 at 7:34 pm #8016grusha.prasadParticipantHi all,
I am working on something similar, but when I use the syntax Jeremy suggested, the statement always evaluates to the else condition. Here is my code and the first few rows of my csv file. Can anyone spot where I am going wrong? For example even when I change the “yes” to “no” only the else statement gets evaluated. This makes me think that my conditional checking is wrong somehow but I can’t spot it.
Thank you!
Grusha.( row.truncated=="yes" ? [ newText("instructions", "<b>Re-type the partial sentence in the prompt and complete it.</b>") .settings.center() .settings.css("font-size", "medium") .print() ] : [ newText("instructions", "<b>Re-type the full sentence in the prompt.</b>") .settings.center() .settings.css("font-size", "medium") .print() ] )
Group,sent_type,struc,prime_type,verb,verb_num,sent_id,sentence,question,response,truncated list_1A,prime,rrc,NA,recorded,1,recorded1,The priest recorded by the mob preached against violence.,no list_1A,prime,rrc,NA,recorded,2,recorded2,The management recorded by the employee talked about the importance of privacy.,no list_1A,prime,rrc,NA,recorded,3,recorded3,The dancer recorded by his trainer practiced energetically.,no list_1A,target,rrc,rrc,recorded,4,recorded4,The influencer recorded,yes list_1A,prime,urc,NA,loved,1,loved1,The landlord who was loved by his tenants was very considerate.,no list_1A,prime,urc,NA,loved,2,loved2,The comedian who was loved by the audience impersonated a famous celebrity.,no list_1A,prime,urc,NA,loved,3,loved3,The kid who was loved by her parents went to bed with a smile.,no list_1A,target,rrc,urc,loved,4,loved4,The janitor loved,yes
- This reply was modified 2 years, 5 months ago by grusha.prasad.
April 7, 2022 at 11:31 am #8020grusha.prasadParticipantOops I figured it out — my CSV file was not structured correctly. Apologies!
-
AuthorPosts
- You must be logged in to reply to this topic.