Need help with Coding Feedback


I am trying to create an experiment with 2 choices on the screen (one on the right side and one on the left) and the user will press the ‘f’ key to choose the left choice and the ‘j’ key to choose the right choice. I am also trying to write feedback that tells the user “Good job” if they clicked on the correct answer, and “No. ___ is the correct answer.” if they clicked on the incorrect key. My code for this is

if 'f':
      feedback_1 = "Good job!"
     feedback_1 = "No. _____ is the correct answer."

(and I use if ‘j’ if the correct answer is on the right side of the screen)
The problem I am running into is that no matter if I press the ‘f’ key or the ‘j’ key the feedback always comes up as the “Good job!” feedback despite if the correct key was pressed. I have tried to play around with the syntax and other functions and am very stuck and would appreciate any help so that the user can receive the correct feedback when they press a key.

Thank you!

The issue here is that your logical test is if 'f': This will always evaluate to True (Python regards strings of characters as True unless they are empty (i.e. '').

Your actual test needs to be more specific (of what key was pressed). Let’s say your Keyboard component was called key_resp, and your code is running in the “End routine” tab, because the component is et to end the routine on a keypress. Then your logical test could be something like:

if 'f' in key_resp.keys:

This will evaluate to True if 'f' was pushed, and False for any other response. So you should make sure that your keyboard component is set up to only accept 'f' or 'j' keys.

But then you run against the issue that I doubt that 'f' is always the correct response. I guess that you have a variable in your conditions file that gives the actual correct response on a trial-by-trial basis? Then in that case, substitute the name of that variable in the test, e.g.

if your_correct_variable_name in key_resp.keys:
1 Like