【问题标题】:Why am I getting a syntax error in python using Eclipse? [duplicate]为什么我在使用 Eclipse 的 python 中出现语法错误? [复制]
【发布时间】:2017-04-27 23:01:28
【问题描述】:

编辑:PYTHON 2.6 ...所以在技术上不是重复的问题.....

我试图弄清楚如何使用this tutorial 在 Ecipe 上使用 Python,但我被困在第 4 部分。调试。 使用此代码:

def add(a,b):
    return a+b

def addFixedValue(a):
        y = 5
        return y +a

print add(1,2)
print addFixedValue(1)

我加了断点,还是报错:

    print add(1,2)
            ^
SyntaxError: invalid syntax

【问题讨论】:

  • 99% 的机会你忘记了前一行的右括号。
  • Python 3 期望 print: print(add(1,2)) 的参数用括号括起来。
  • 提供您的完整代码
  • 你确定你已经在 Python 2 中试过了吗?
  • 如果它只列出 Python 3,我会说你只能在 Python 3 下运行脚本,这是唯一的选择,默认情况下是有意义的。如果要使用 Python 2 运行脚本,则需要安装 Python 2。

标签: python eclipse debugging


【解决方案1】:

根据python 2,您的代码是正确的。

当您收到syntax 错误时,您必须使用python 3。在这种情况下,您需要在print 语句周围添加括号

print (add(1,2))

【讨论】:

  • 添加了截图。绝对是 Python 2。我想我可以只使用 Python 3,但我仍然想知道为什么它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
相关资源
最近更新 更多