2016-11-01 5 views
-2

Wenn ich unter Code ausführen, bekomme ich "Syntaxfehler: ungültige Syntax". Wenn ich diesen Code jedoch nach dem Drucken mit Klammern ausführe, erhalte ich die richtige Antwort.Drucken funktioniert nicht ohne Klammern (Python 2.7)

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 

ich hinzufügen möchte, dass das ich in diesem Notebook lief nicht die einzige Sache ist, ich habe (unter anderem) einige Pakete enthalten:

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 

Am Ende würde ich gerne hinzuzufügen, dass in einer neuen Arbeitsmappe alles korrekt funktioniert. Hat jemand eine Idee, was kann der Grund dafür sein?

Antwort

6

Dies verursacht das Problem: from __future__ import print_function.

Das bedeutet, dass Sie die neuen print_function von Python 3 in Python 2.7 importieren. Daher sind Klammern erforderlich. Mehr unter future imports.

Verwandte Themen