0

When the following code is executed line by line on shell, it is giving the expected output :

>>> name=input("Please give your full name : ")
Please give your full name : ins vikranth
>>> ListName=name.split(' ')
>>> outputString=ListName[1]+' '+ListName[0]
>>> print(outputString)
vikranth ins

The code is not running in total as a file but runs line by line on the shell. The code is :

name=input("Please give your full name : ")
ListName=name.split(' ')
outputString=ListName[1]+' '+ListName[0]
print(outputString)

The error message is :

Please give your full name : ins vikranth
Traceback (most recent call last):
  File "ReverseName.py", line 1, in <module>
    name=input("Please give your full name
  File "<string>", line 1
    ins vikranth
               ^
SyntaxError: unexpected EOF while parsing

Why is this happening?

Vikranth Inti
  • 125
  • 5
  • 13
  • 2
    Your code works for me (Python 3.7.2), without error. How are you running the file `ReverseName.py`? – adrianus Mar 15 '19 at 08:39
  • Well, I might be missing something, but it is not reproductible on my side, how do you launch it? – BlueSheepToken Mar 15 '19 at 08:42
  • `unexpected EOF while parsing` means there is a string that is not quoted properly. Are you not missing a `"` on the `input()` row ? –  Mar 15 '19 at 08:46
  • @adrianus I ran the program as $python ReverseName.py and got the error. This time I tried $python3 Reversename.py and everything went fine. – Vikranth Inti Mar 15 '19 at 09:07

2 Answers2

2

the reason why this is happening is your python version ... your IDLE is python 3.X while your file is being "translated" ( Interpret ) using python 2.X ... so there are 2 simple solutions:

1/ stick with python 3.X - your code is not going to change, just change Interpreter

2/ edit it to be compatible with python 2.X :

name=raw_input("Please give your full name : ")

also here are 2 online compilers, where you can see the difference:

Python 3.7 -> https://www.onlinegdb.com/online_python_compiler

Python 2.7 -> https://repl.it/languages/python

StyleZ
  • 1,276
  • 3
  • 11
  • 27
-1

@Babu brother code seems to okey for me but you might forget somepoint.If person have a middle name it will cause an error. You have 2 array locations when user types more than 2 words it cause an unexpected end of file due to lack of location like Kevin Prince Boateng a footballer. You might want to look this https://docs.python.org/2/c-api/memory.html for a dynamical memory management

A.Toraman
  • 15
  • 5
  • my code is accepting 4 words and giving output as expected. $Please give your full name : abc pqr xyz lmn>> lmn xyz xyz abc – Vikranth Inti Mar 15 '19 at 09:11
  • name=input("Please give your full name : ") ListName=name.split(' ') outputString=ListName[3]+' '+ListName[2]+' '+ListName[2]+' '+ListName[0] print(outputString) @A.Toraman – Vikranth Inti Mar 15 '19 at 11:41