darkstarohio darkstarohio -4 years ago 56
Python Question

Python Error when splitting a string - not sure how this is possible

I am trying to split a string of numbers, and store each number in it's own variable. I've searched google and here for answers, and everyone seems to have the same response, but it will not work for me.

Here is the code I am using:

Input = '1 2'
x, y = Input.split()


The strange thing is that I get the following error:

x, y = Input.split()
^
SyntaxError: invalid syntax


When running in interactive mode it works without issue.

I've tried:


  • Changing from Python 2.7 to Python 3.6

  • Changing to 64 bit Python

  • Changing variable names

  • Assigning to a list, rather than individual variables. Same error.



I'm at a complete loss as to what's happening to make this not work in a .py file, as opposed to interactive mode. Thoughts?

Answer Source

I can't add comment here but mostly this issue is due to indentation error. and if you are still facing problem then send some snapshots or try executing below code:

input = '1 2 3'
x, y, z = input.split()
print(x)
print(y)
print(z)

there is no indentation in any line

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download