脚本在shell中工作,但不在IDLE文件中

问题描述 投票:0回答:2

当在shell上逐行执行以下代码时,它会给出预期的输出:

>>> 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

代码不是作为文件一起运行,而是在shell上逐行运行。代码是:

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

错误消息是:

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

为什么会这样?

python shell command-line eof
2个回答
2
投票

发生这种情况的原因是你的python版本...你的IDLE是python 3.X,而你的文件是使用python 2.X“翻译”(解释)...所以有2个简单的解决方案:

1 /坚持使用python 3.X - 您的代码不会改变,只需更改Interpreter

2 /编辑它以与python 2.X兼容:

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

这里还有2个在线编译器,您可以在其中看到差异:

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

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


-1
投票

@Babu兄弟代码对我来说似乎很有用,但你可能会忘记某些问题。如果有人有中间名,就会导致错误。当用户输入超过2个单词时,你有2个数组位置,由于缺少凯文王子博阿滕足球运动员的位置而导致意外的文件结束。您可能希望将此https://docs.python.org/2/c-api/memory.html用于动态内存管理

© www.soinside.com 2019 - 2024. All rights reserved.