psychopy.org | Reference | Downloads | Github

If condition not working with input from keyboard component

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

OS Windows 10
PsychoPy version 1.85.6:
**Standard Standalone? yes

Hello everyone! Please forgive me if this has been answered before, I’ve hunted through the forum and couldn’t find this issue.

I’m programming a button choice experiment with two button, on the right and on the left. When the participant presses the right arrow key, it selects the right-hand button on the screen` when they presses the left arrow key, it selects the left-hand button on the screen.

I want a number to appear on the button they selected. Pretty straightforward, right? But for some reason, I can’t make this work! The button on the left disappears no matter which button I press - the if condition isn’t working. The output file shows that the key presses are being received and save correctly by Psychopy.

I inserted this code component:

if key_resp_1.keys[0] == 'right':
    rightB.opacity = 0
    leftB.opacity = 1
else:
    leftB.opacity = 0
    rightB.opacity = 1

I receive this error:

UnicodeWarning: Unicode equal comparison failed to convert both arguments to unicode, interpreting them as being unequal.

I have tried all sorts of things to encode either the keyboard’s input or the ‘right’ string in the comparison, and debugging shows that I have successfully encoded them as either ascii or unicode, but the error keeps appearing.

Does anyone have any idea why this is happening? Am I using the keyboard component incorrectly?

Done, thanks for the feedback! As you may be able to tell, I’m new :slight_smile:

Add this before your if statement:

print(key_resp_1.keys[0])
print(type(key_resp_1.keys[0]))

and tell us what you get.

Oh my goodness, I found the problem thanks to that code! It was recording ‘r’ instead of ‘right’. Thank you!