KSully2
KSully2

Reputation: 149

python IDLE vs Mac Terminal print statement

I'm writing a really simple tic-tac-toe program to teach myself Python. I've just begun learning to use command line as well, and I've noticed some different behavior with the print statement when I run my code in IDLE or using Mac Terminal. I'm using Python 3.4.

The screenshotted output is coming from this function:

def print_board(board):
    for row in board:
        row = " ".join(row)
        print(row)
    print()

The final print statement is there to create a blank row. When the code is run in IDLE, it works as expected (see left side of screenshot), but when run in Mac terminal, it prints the parentheses from the function call.

Can anyone explain to me why this is happening?

Left is IDLE, right is Mac Terminal

Upvotes: 1

Views: 718

Answers (2)

A.J. Uppal
A.J. Uppal

Reputation: 19274

Your default python version in your Mac Terminal is , which does not take print as a function. Thus, it prints the argument, an empty tuple (), out.

:

>>> import sys;sys.version
'2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> print()
()
>>> 

:

>>> import sys;sys.version
'3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan  5 2014, 11:02:52) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> print()

>>> 

Upvotes: 2

Wombatz
Wombatz

Reputation: 5458

That is because in python 2 print is not a function, but a statement. So writing

print()

Is actually

print ()

Which means: print an empty tuple. So your terminal obviously uses python 2. You can try to run your script with python3. Your IDE uses python3 where print is a function and print() means: print nothing

Upvotes: 2

Related Questions