Code:

a=input().split() print(a) 

When submitting to the input line '1 2 3'

 Traceback (most recent call last): File "", line 1, in <module> a=input().split() File "<string>", line 1 1 2 3 ^ SyntaxError: invalid syntax 

The expression is taken from the example, it should work. What could be the snag?

  • 2
    In Python 3, everything is fine, in the second one there really is such a problem. Are you using a second python? If so, indicate this in the question (and the tag) so that there will be no misunderstandings later. - Flowneee
  • If an example is from some book, then it is better to put the third python instead of the second one, and then there will be even more inconsistencies in the future - Andreymal

2 answers 2

This is not split - this input throws an exception. Why all? Goodness. The documentation says this - input does two things:

1) Reads from stdin.

2) Everything that is read is executed ( eval ).

Accordingly, the string 1 2 3 could not be executed due to incorrect syntax. In Py3, the behavior of input() changed so that people no longer get confused. Use raw_input . Or Python3. And you can also use the six library so that if you suddenly move to another version, your code will not break, because there is no raw_input() function in raw_input() .

    Just replace the input() method with raw_input()