Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :(

like image 439
Randex Avatar asked Dec 06 '25 04:12

Randex


2 Answers

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!"
like image 174
The Unfun Cat Avatar answered Dec 08 '25 18:12

The Unfun Cat


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.

like image 37
asmeurer Avatar answered Dec 08 '25 16:12

asmeurer