-2

So, I wrote this simple piece of code which takes a list of lists as an argument and sums each sublist's items.

def addItems(li):

    for k in li:
        sum = 0
        for i in k:
            sum += i

        print " + ".join(["%d" % (i) for i in k]) + " = %d" % (sum)

When I try to import the module in python2.7, I succeed.

However, when I try to do the same in python3.5, it brings up this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/george/Desktop/random/pyproj/myLibs/firstLib.py", line 9
     print " + ".join(["%d" % (i) for i in k]) + " = %d" % (sum)
               ^
SyntaxError: invalid syntax
docff
  • 23
  • 4
  • 1
    In short. In Python 3, `print` is now a function, so you have to call it as such: `print('things go in here')`. – idjaw Sep 07 '16 at 01:02
  • Although not a duplicate, it did answer my question! Thank you! – docff Sep 07 '16 at 01:03
  • 3
    It definitely is a duplicate. It works in Python 2 because `print` is a statement in python 2. The parentheses are not needed. In Python 3 you have to use it because `print` is a function. That is *exactly* what that duplicate link explains. https://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function – idjaw Sep 07 '16 at 01:04

1 Answers1

0

In Python 3, print is a function, not a statement. You need parentheses around what you want to print.

Also, before you claim that something doesn't work, you should prove, on the interactive command line and with a minimal example, that that really is the thing that doesn't work.

D-Von
  • 416
  • 2
  • 5