EOFError when using walrus operator

sam_dict = {
    "mouth": "Mund",
    "finger": "Finger",
    "leg": "Bein",
    "hand": "Hand",
    "face": "Gesicht",
    "nose": "Nase"

while ((entry := input('Enter a word in English or EXIT: ')) != 'Exit'):
  if entry in sam_dict:
    print('Translation:', sam_dict[entry])
  elif entry not in sam_dict:
      print('No match!')

gives the error,

Enter a word in English or EXIT: 

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
EOFError: EOF when reading a line

I setup python 3.11 (and also python 3.10) on colab, and got this error.
the same code works on PyCharm, with python 3.10

it turns out that

x = input('enter: ')

this also gives the same error, I think so the problem is with taking an input with %%python3 enabled

Hello, @vainaixr:

Could you give us a hint about the environment you are using. Is it Windows? Linux? macOS? Which version?

What do you mean by “walrus”? I do not recognise the reference.

What do you mean by “%%python3”? I know that one can run the Python interpreter from a Linux or macOS terminal using the command “python3”, and that the command prompt in this circumstance is sometimes “%”, but I do not understand “%%python3”.

I ran the code on google colab, with,

Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.5 LTS
Release:	18.04
Codename:	bionic

walrus is described here,

I have setup python 3.11 on colab, by default colab uses a different version of python, to run cells with python 3.11, I use the magic command


I think the double % is to indicate multiple lines need to be run with python 3.11.
but it gives an error when taking an input.

Ah! I think I begin to understand.

It sounds like you are writing some Python code in a Google Colab notebook. You want your code to read input from the user. You have some example code which reads input using an assignment expression (which has the := operator, sometimes nicknamed “walrus”). The expression raises an EOFError.

You have simplified your code to an assignment statement which reads input. This gives the same error.

Your example of this code is in the context of a colab cell, where you use the cell magics %%python to get the Python 3.11 interpreter.

Did I understand all that correctly?

I suspect the fundamental question is: how best to get user input into a Google Colab cell for your code to work on? Sorry, I don’t know enough about Google Colab to answer that question. Maybe post that question and see what answers you can find. Perhaps this is a good place to get answers about Google Colab. Maybe some other forum knows as much about Google Colab as this forum knows about Python.

When I look at what cell magics actually do, the EOFError does not surprise me. The %%python3 cell magic is a shortcut for a %%script python3 cell magic. What that magics seems to do is to run the code in the cell as a subprocess. I do not see a way to pass data to that subprocess via its standard input (“stdin”). But when Python runs the input() function, it probably reads from standard input. Thus the empty standard input immediately delivers an end of file indicator: EOFError.

Count your parenthesis.

Note that notebooks do tricks to make stdin, stdout and stderr working inside the notebook and you can encounter multiple problems with that. For example buffering is often not handled correctly.

You can notice that input behaves very differently inside notebooks. Namely inside Google Colab it is this function (not the standard Python function):

<bound method Kernel.raw_input of <google.colab._kernel.Kernel object at 0x7f03d312cd10>>