Integrating Python code with input values to PsychoPy

Hi! I have a piece of code that I am trying to integrate into my experiment. Basically the code generates a random number, displays it on the screen, and then has users input the generated number in order to move forward. While this code works perfectly fine on PyCharm runner, I can not quite figure out how to get it to work on PsychoPy. The error code I am getting is

user_guess = int(input(f"Enter the number {random_number}: "))
EOFError: EOF when reading a line
################# Experiment ended with exit code 1 [pid:9812] #################

I will also include a picture of the code.

Any help would be greatly appreciated!

I’d assume Psychopy’s console has no support for input.
For I/O you would generally use one of Psychopy’s components (most likely the textbox component in your case).

I suggest using Psychopy’s builder with a textbox component (check “editable” in its settings) and a code component, where you insert the logic.

The following assumes that the textbox is calles “textBox”.

Something like this in the Begin Routine tab:
random_number = str(randint(1000, 9999))
(this generates a random number and converts it to string datatype)

On a second routine you can create two text components: One for the message when it’s correct and one for the message when incorrect. Change “start (time)” to “start (condition)” and put either textBox.text.strip() == random_number and textBox.text.strip() != random_number for the start conditions.
(this strips the input from whitespaces and checks against the generated random number)

This is very rudimentary (you might want to add a button to continue the next routine, etc).

//edit: you can put a loop around the two components and - in the case of a correct guess - break out of the loop with another code component.
Breaking out of the current loop can be achieved by using currentLoop.finished = True


Hi umb, I tried that and I am still getting the same error code. It seems it still doesn’t like me using the input command from Python. The only thing I did to the textbox component was check editable, is there maybe something else I need to do for that. Also I have the code I submitted above in the each frame section tab of coder, and I added the code you suggested to the begin routine tab.

Psychopy’s standalone is not made in mind with console input.
Unless you run the experiment from within another console that supports console input this will probably not work.

Is it important for you to use the console as a mean for input/output?
Using Psychopy’s components for input/output seems more practical to me.


It does not matter what console I use. However I am not sure how I would build this using purely Psychopy components. Any tips would be greatly appreciated!

@umb is not suggesting that you don’t use any code, just that you use PsychoPy components (such as editable textboxes and keyboard components) instead of input.

Other code not suitable for PsychoPy Builder experiments are flip and wait. I also tent to avoid while.


The idea is to replicate your python logic with psychopy’s own components in the builder.
So you replace the the input()-function with psychopy’s textbox component. You replace the while-loop with psychopy’s loop. You replace the print()-function with psychopy’s text-component. Etc.
You may still use custom code by inserting it with psychopy’s code components (like the random number generation). But the flow and I/O should use psychopy’s components.

I attached a minimal working exampl of how this could be constructed within psychopy.

untitled.psyexp (22.3 KB)

1 Like

Thank you so much, you are a saint!