从Sublime Text运行Python 3时出错,适用于IDLE

时间:2014-07-04 20:01:22

标签: python macos sublimetext2

我有:

def confirm_title(title):
    print("Is '", title, "' correct?", sep='')
    confirmation = input("Y = yes N = no: ")
    return confirmation

在sublime text 2中并使用以下命令运行它:

  

工具 - >构建系统 - > (选择)Python
   CMD + B (OSX)

sublime文本控制台给了我以下错误:

File "/Users/robert/Desktop/Python/ex_1.py", line 69
    print("Is '", title, "' correct?", sep='')
                                          ^
SyntaxError: invalid syntax
[Finished in 3.6s with exit code 1]
[shell_cmd: python -u "/Users/robert/Desktop/Python/ex_1.py"]
[dir: /Users/robert/Desktop/Python]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

现在如果我在python IDLE 3.4上运行它,它就可以了。为什么在崇高的文本中它不是没有?是因为它使用了预装的其他版本的python还是什么?

1 个答案:

答案 0 :(得分:1)

这是SyntaxError是triggrered,因为你在Sublime中使用Python 2.x。

您可以通过例如

确认

通过简单执行一个

print "hello world"

如果这样做,你可以确定你没有使用Python 3,而是使用Python 2.或者只是导入

from platform import python_version
print python_version

Python 2通常支持将print作为函数调用,但是当您尝试打印元组时,结果会变得很丑陋。

print 'Hello, World!'
print('Hello, World!')

Python 2.7.6
Hello, World!
Hello, World!


print 'Python', python_version()
print('a', 'b')
print 'a', 'b'

Python 2.7.6
('a', 'b')
a b

你不能在Python 2中使用end参数。如果你想在同一行上打印,你可以使用它:

print "text", ; print 'print more text on the same line'

text print more text on the same line