【问题标题】:(python) ERROR: attempted use of x = input("string") causes errors(python) 错误:尝试使用 x = input("string") 会导致错误
【发布时间】:2020-05-05 20:01:50
【问题描述】:
Traceback (most recent call last):
File "p.py", line 1, in <module>
x = input("hello")
File "<string>", line 0  

SyntaxError: unexpected EOF while parsing

当我在脚本中输入以下代码时出现此错误:

x = input("press enter to continue")

当我输入这个时也是这样:

input("press enter to continue")

再说一遍:

input('press enter to continue')

我在带有 linux beta 的 chrome acer 上运行 linux。它使用python 3。出了什么问题?

【问题讨论】:

  • 你确定你使用的是python 3吗?
  • 我在终端中输入了 python 3,然后它就可以工作了,但不是作为脚本的一部分。

标签: python python-3.x linux google-chrome chromebook


【解决方案1】:

我相信您使用的是 Python 2,而不是 Python 3。将 input 替换为 raw_input:

x = raw_input("press enter to continue")


这就是我相信您使用 Python 2 的原因。我使用 Python 3 进行的测试(注意没有错误):

Python 3.7.6
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input("press enter to continue")
press enter to continue
>>>

然后我看到了与使用 Python 2 时相同的错误:

Python 2.7.17
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input("press enter to continue")
press enter to continue
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing
>>> x = raw_input("press enter to continue")
press enter to continue
>>>

【讨论】:

  • 在终端中运行和在脚本中运行时存在差异
  • @joshuarobbins 你能告诉我们脚本的其余部分吗?
  • 字面意思就是这一行:input("continue")
  • 根据您的评论 below,您使用的是 Python 2.7.13,因此如果您将 input 更改为使用 raw_input,您的脚本将正常工作
  • 原来IDE没有在更新的python版本上使用,导致错误。
【解决方案2】:

尝试 raw_input() 而不是 input()

【讨论】:

  • raw_input 用于 python 2。他正在使用 python 3。
  • 它确实有效。你如何检查它正在使用什么,因为我以为我安装了 python 3
  • 我相信他正在使用 python 2.7 否则他不会得到这个错误。
  • 在终端中输入:python --version 然后输入:python3 --version
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2013-12-12
  • 2020-12-17
  • 2023-03-20
  • 2019-06-16
相关资源
最近更新 更多