-2

When I run below code, I get "Syntax Error: invalid syntax". However, if I run this code with parentheses after print, I get the correct answer.

liczby = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
743, 527]
for x in liczby:
    if x == 237:
        break
    if x % 2 == 0:
        print x

I would like to add, that this is not the only thing I ran in this notebook, I have (among other) included some packages:

from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import tarfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle

At the end, I would like to add, that in a new workbook everything works correctly. Does anybody have an idea, what can be the reason for this?

Kuba
  • 291
  • 2
  • 5
  • 14

2 Answers2

6

This is causing the problem: from __future__ import print_function.

This means that you are importing the new print_function from Python 3 into Python 2.7. Hence, parenthesis are required. More on future imports.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

It shouldn't be able to print without parentheses since you're importing the print statement from python 3 from __future__ import print_function https://docs.python.org/2/library/functions.html#print

ink
  • 159
  • 2
  • 10