Randex
Randex

Reputation: 800

Python print function

I began learning Python a little time ago, and I got the first problem. Here's the code:

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

When I execute it in the Terminal (OS X), it tells me invalid syntax error where end equal sign is placed. What's the problem here? Didn't find the solution...

I installed Python 3.3.0 from this page, Python 3.3.0 Mac OS X 64-bit/32-bit x86-64/i386 Installer

Sorry for so nooby questions :(

Upvotes: 3

Views: 3204

Answers (2)

The Unfun Cat
The Unfun Cat

Reputation: 31898

Your terminal is probably using Python2.x

Try using the command python3 yourfilename.py

To see which Python version is the standard on your terminal just type python

You should see something like this:

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.

To make your code work with 2.x you can use print without the parantheses:

print "Python", "yay!"

Upvotes: 6

asmeurer
asmeurer

Reputation: 91450

The Python 3 installer does not make Python 3 the default Python (if it did, it would break a ton of stuff, because very few Python scripts are Python 3 compatible). So to get Python 3, you should execute your script as python3 script.py, or add #!/usr/bin/env python3 to the top of the file.

Upvotes: 5

Related Questions