0

I am new to python. I searched and found out how to do it in python 3:

v = [int(x) for x in input().split()]

but in python 2 I get the syntax error. I need it for foobar because I can't code in c++ nor python 3 there which is incredibly annoying.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128

3 Answers3

0

In Python 2 you have 2 input functions:

  • raw_input : this does what you expect; it reads the input as a string.
  • input : this actually evaluates the user input; this is why you get a syntax error; you cannot evaluate a string like "1 2 3 4 5".

So you want to use raw_input():

v = [int(x) for x in raw_input().split()]

In Python 3 input instead does what you want.

Luca Angioloni
  • 2,243
  • 2
  • 19
  • 28
0

Try this:

v = [int(x) for x in raw_input("Enter your numbers separated by space: ").split()]

In Python 2.X

  • raw_input() takes exactly what the user typed and passes it back as a string
  • input() first takes the raw_input() and then performs an eval() on as well.

In Python 3.X

  • raw_input() was renamed to input() so now input() returns exactly what the user typed and passes it back as a string
  • Old input() was removed.
Orion
  • 248
  • 3
  • 10
0

In python3 there is no raw_input. The python3 input is the renamed python2 raw_input. So in python2 you should use raw_input instead. Like this:

v = [int(x) for x in raw_input("Enter numbers here").split(" ")]

The argument in split is the separator. In the first example the separator is a space. But if you want to get the numbers separated with -, you can use it like this:

v = [int(x) for x in raw_input("Enter numbers here").split("-")]
Mark7888
  • 823
  • 1
  • 8
  • 20