#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# [solved] Putting "if-clauses" in the Key-response

edited November 2011
Hey,

In the experiment i designed there is no fixed correct response. Is there any possability to put a "if-clause" like in python in Open Sesame, so he logs "yes" IF the response was "districtor1" (so a number between 1 and 4). At the moment it logs the response and i can manually check if the response was correct, but if i test around 50 people it would be nice if Open Sesame would be able to just log if the response was correct.

So there are 4 pictures in random order shown and i want to log the correct response which is one of the pictures.

• edited 8:47AM

The easiest way to achieve this is probably to add an inline_script item (somewhere after the keyboard_response and before the logger) with something like the following code in the run phase:

if self.get("response") in ["1", "2", "3", "4"]: self.experiment.set("my_response", "distractor") else: self.experiment.set("my_response", "target")

This will create a new variable "my_response" which is set to "distractor" if the response was "1" through "4" and set to "target" otherwise.

Hope this helps!

• edited 8:47AM

Hey,

I have changed the picture_selecting so that "dist1" is the correct response. "dist1" gets shuffeled with "dist2-4" so the correct response is a number btw. 1 and 4 but the variable "dist1".

Is there a possability to set
correct == 0
if "dist1" set correct = 1 ?

Best regards and thanks again for your support

Henrik

P.S.: I found a bug, if you enter an "illegal" command and Run the script after you get a bugreport. If you close Open Sesame than, without removing the bug, the script can´t be opened again, because it tries to immidiatly run the script which isn´t possible. In that case you ain´t able to open the script to edit it.

• edited 8:47AM

I'm afraid I don't quite follow, particularly this bit:

so the correct response is a number btw. 1 and 4 but the variable "dist1".

Could you perhaps describe in a bit more detail what you have done and what you want to achieve?

P.S.: I found a bug, if you enter an "illegal" command and Run the script after you get a bugreport. If you close Open Sesame than, without removing the bug, the script can´t be opened again, because it tries to immidiatly run the script which isn´t possible. In that case you ain´t able to open the script to edit it.

Thank you! That's a serious bug indeed. It would be very helpful if you could describe the exact steps that will reproduce the problem (either here or on the issue tracker).

Btw, perhaps it's good to know that you can open the .opensesame.tar.gz files with a program like WinRar. So even if OpenSesame fails to open the files (which it shouldn't, of course), you can still edit the experiment.

• edited 8:47AM

Hey,

to part2 yes i found that out quite soon, so it wasn´t such a loss.
The exact problem was that i entered a line in a sketchpad ,and saved it, that couldn´t be executed. After i got the bugreport i closed OpenSesame. As i tried to open the Script, OpenSesame immidiatly tried to run the Script, which wasn´t possible. It gave me the bugreport but didn´t open the script, so i couldn´t remove the false line.

To my problem discription:

The programm now shuffles 100 numbers, each number has 5 pictures.
Than draws 40 of them and displays them in the 2 different phases, the pictures names are for example 41a, 41b, 41c, 41d, 41e. In the display phases Picture 41a and Picture 41b get displayed.
In the recall phase picture 41a gets displayed on top off the other 4.
Picture 41b gets shuffeled into the 3 distractors (41c, 41d, 41e). I defined the "correct" picture as "dist1". But since they get shuffeled it is allways a different number.
I hope i managed it to explain it now, my english isn´t perfect ;(.

Best regards

Henrik

• edited 8:47AM

Ah, I think I understand now. There is a variable "dist1", which has a value between 1 and 4, depending on where the target picture was presented. So "response" has to match "dist1". Is that correct?

There are two ways to do this:

1) The easiest way is probably to enter [dist1] in the correct response field of the keyboard response.

2) A more flexible way is using an inline scipt. Something like this should do the trick:

if self.get("dist1") == self.get("response"): self.experiment.set("correct", 1) else: self.experiment.set("correct", 0)

The exact problem was that i entered a line in a sketchpad ,and saved it, that couldn´t be executed. After i got the bugreport i closed OpenSesame. As i tried to open the Script, OpenSesame immidiatly tried to run the Script, which wasn´t possible. It gave me the bugreport but didn´t open the script, so i couldn´t remove the false line.

Could you perhaps be even more specific? In order to address the issue I really need a step-by-step description of what you did and what happened as a result, including the exact error messages and the line of script that causes the problem. Thanks!

• edited 8:47AM

Okay,

exact discription is: (Only tried it with Sketchpads so far but should happen on all in-line scripts).

"Error: Script error
Description: Failed to instantiate module 'sketchpad' as 'zweiterTeil': Error: Script error
Description: Unknown command ' # cookies'"

If you enter in a sketchpad # (text) and try to run it you will get that bug. If you saved before you run it and close the programm while the "bug-window" is open, OS will try to run the script and instead of opening your script will give you this bugdiscription:

"Error: Failed to open 'C:/Users/Henrik Ho/Desktop/Uni/Praktika/erstes Praktikum Pulsschlag-Zeit bei Leistung/es-geht!.opensesame.tar.gz'
Description: Error: Script error
Description: Failed to instantiate module 'sketchpad' as 'zweiterTeil': Error: Script error
Description: Unknown command ' # cookies'

Make sure that the file is in .opensesame or .opensesame.tar.gz format. If you should be able to open this file, but can't, please go to http://www.cogsci.nl/opensesame to find out how to recover your experiment and file a bug report."

Without opening the script.

Okay and for the "if-clause" i think i havn´t defined the variable correctly.

target = num + "a.bmp"

dist1 = num + "b.bmp"
dist2 = num + "c.bmp"
dist3 = num + "d.bmp"
dist4 = num + "e.bmp"

That is how i have defined it. Picture with "b" is allways correct. But if i enter your suggested textline or keyboard response i get a bug, saying:

"Error: Inline script error
In: inline_script (prepare phase)
File "libopensesame\item.pyo", line 302, in get

Python traceback:
runtime_error: Error: Runtime error
Description: Variable 'dist1' is not set in item 'inline_script'."

You are trying to use a variable that does not exist. Make sure that you have spelled and capitalized the variable name correctly. You may wish to use the variable inspector (Control + I) to find the intended variable.

Full traceback in debug window"

Best regards

Henrik

Once more thanks for your really good support!

• edited 8:47AM

It's important to know that not all Python variables are automatically recognized by OpenSesame with the "[variable]" notation. Specifically, OpenSesame only recognizes variables that are properties of the experiment object. In practice, this means that you should use the get() and set() functions to manipulate variables that you want to use elsewhere in OpenSesame.

dist1 = num + "b.bmp" # won't work self.experiment.set("dist1", num + "b.bmp") # will work print dist1 # won't work print self.get("dist1") # will work