My Code component give me the wrong result n matter what I do. How can I fix it?

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Windows
PsychoPy version (e.g. 1.84.x):

What are you trying to achieve?:
In my experiment the participant must click on a point on a slider scale , where there is a topic on each end. Wherever they click , the topic that is closest (for some conditions) or farthest (for one condition) on the scale get recorded for use for a later part of the experiment. the slider goes from -3 to +3

What did you try to make it work?:
I used this coding component pasted below:

if Type=='C':
    if Choose_slider.getRating() is not None and Choose_slider.getRating() >=3:
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==2:
        Answer=ChoiceA
    else: 
        Answer=ChoiceB
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(1):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-1):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-2):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-3):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
elif Type=='HI':
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==3:
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==2:
        Answer=ChoiceA
    else: 
        Answer=ChoiceB
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(1):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-1):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-2):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-3):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
elif Type=='LI':
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==3:
        Answer=ChoiceA
    else: 
        Answer=ChoiceB
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==2:
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(1):
        Answer=ChoiceB
    else: 
        Answer=ChoiceA
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-1):
        Answer=ChoiceA
    else: 
        Answer=ChoiceB
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-2):
        Answer=ChoiceA
    else: 
        Answer=ChoiceB
    if Choose_slider.getRating() is not None and Choose_slider.getRating() ==(-3):
        Answer=ChoiceA
    else: 
        Answer=ChoiceB

What specifically went wrong when you tried that?:
The code works and prints out the results at the end. The if statements with the negative numbers print out the correct prompts, but the if statements with the positive numbers prints out the opposite/wrong prompts.

I tried flipping what it should print as the answer , but it still prints out the same result

I am not sure what to do next. Please help?

Is it true for conditions C and HI that positive ratings should result in ChoiceB and negative ratings in ChoiceA? But for condition LI it should be reversed?

If I understand you correct:

Your code is not doing what you think it does. For each type (because of the structure of your code) only the last if/else statement is determining the outcome. In essence: Drop all else blocks.

You may want to make your code more readable, though. If my assumption from above is correct, you can very much write it like this:

if Type == "C":
    if Choose_slider.getRating() > 0:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "Hi":
    if Choose_slider.getRating() > 0:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "LI":
    if Choose_slider.getRating() > 0:
        Answer=ChoiceA
    else:
        Answer=ChoiceB

This code is not tested, but you get the idea. If it is a binary decision, you only need one if/else statement. You may want to check for None as well.

2 Likes

You understand correctly! I had to account for β€˜None’ and got this code:


if Type == "C":
    if Choose_slider.getRating() is not None and  Choose_slider.getRating() < 0:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "Hi":
    if Choose_slider.getRating() is not None and  Choose_slider.getRating() < 0:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "LI":
    if Choose_slider.getRating() is not None and  Choose_slider.getRating() < 0:
        Answer=ChoiceA
    else:
        Answer=ChoiceB

However, testing this code results in the same problem: The if statements with the negative numbers print out the correct prompts, but the if statements with the positive numbers prints out the opposite/wrong prompts; and vice versa when I change the β€œ<β€œ sign to β€œ>”.

I am unsure of what to do next as a result.

I figured out the issue! It is not the labels that I have to use, but the ticks. So the tick for the number β€œ0” is 4 , because it is the middle value on my scale! Here is the final code:


if Type == "C":
    if Choose_slider.getRating() is not None and  Choose_slider.getRating() < 4:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "HI":
    if Choose_slider.getRating() is not None and  Choose_slider.getRating() < 4:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "LI":
    if Choose_slider.getRating() is not None and  Choose_slider.getRating() < 4:
        Answer=ChoiceA
    else:
        Answer=ChoiceB

Thank you @umb for the initial reply!