Python打印功能

Ran*_*dex 2 python printing python-3.x

我刚开始学习Python,我遇到了第一个问题.这是代码:

fh = open('/usr/share/dict/words')
for line in fh.readlines():
    print(line, end='')
Run Code Online (Sandbox Code Playgroud)

当我在终端(OS X)中执行它时,它告诉我invalid syntax错误end放置了等号.这有什么问题?没找到解决方案......

我从这个页面安装了Python 3.3.0 ,Python 3.3.0 Mac OS X 64位/ 32位x86-64/i386安装程序

对不起这么吵闹的问题:(

The*_*Cat 5

您的终端可能正在使用Python2.x.

尝试使用该命令 python3 yourfilename.py

要查看哪个Python版本是终端上的标准,只需键入即可 python

你应该看到这样的东西:

Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Run Code Online (Sandbox Code Playgroud)

要使代码与2.x一起使用,您可以在没有parantheses的情况下使用print:

print "Python", "yay!"
Run Code Online (Sandbox Code Playgroud)


asm*_*rer 5

Python 3安装程序不会使Python 3成为默认的Python(如果确实如此,它会破坏大量内容,因为很少有Python脚本与Python 3兼容).因此,要获得Python 3,您应该执行脚本python3 script.py,或者添加#!/usr/bin/env python3到文件的顶部.