| Reference | Downloads | Github

AUTO -> JS Problem: str.endswith (variable, "_ending.png")

There seems to be a problem with the auto conversion from Python into JS.

In Python I use the method:

if str.endswith (variable, “_ending”):
variable_2 = 1

in order the construct a conditional argument based on the image that is shown (red or green background) and the response of the participant.

The auto conversion doesn’t seem to work correctly though, as I keep receiving the error message shown below.

See the code and conversion here:

if str.endswith (animals, “_01.png”):
animal = 1
elif str.endswith (animals, “_02.png”):
animal = 2

if str.endswith (fixation_cross, “red_green.png”):
fixation = 1
elif str.endswith (fixation_cross, “green_red.png”):
fixation = 2

if (str.endswith(animals, “_01.png”)) {
animal = 1;
} else {
if (str.endswith(animals, “_02.png”)) {
animal = 2;
if (str.endswith(fixation_cross, “red_green.png”)) {
fixation = 1;
} else {
if (str.endswith(fixation_cross, “green_red.png”)) {
fixation = 2;

My understanding is that str.endswith looks like a method which would operate on a variable called str

If str.endswith() is a function then it seems oddly constructed and I’m not surprised that it isn’t getting translated correctly. Is there another way to do the same function in Python?

Hey @wakecarter,

thanks for the reply.

so I have tried that and then receive the following error message, even though the path from the excel file clearly links a an image (.png file) and it seems to work for other paths from the same excel file:

And this works perfectly fine in PsychoPy on my laptop.

I think the error is because you are using the Python builtin str rather than using the actual string and endswith methods. This translates directly over to JS, where str does not exist. Replace with fixation_cross.endswith("red_green.png"). Note, this will also fail to translate correctly, as the JS method uses mixed case on the string method, and should be the following in JS fixation_cross.endsWith("red_green.png") - see capital W.

Correct, I ended up fixing this particular problem but right away ran into the next one.

Thanks for the advice, the case-sensitivity troubled me for a few runs as well :slight_smile: