Reputation: 135
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?
Upvotes: 0
Views: 591
Reputation: 1273
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
Upvotes: 2
Reputation: 15
@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
Upvotes: -1