N. Coco
N. Coco

Reputation: 21

Syntax Error Unseen Python 3.4.4

I have been given a hand-me-down code and am trying to incorporate it into a new code I am writing in an attempt to save time. The code itself was written in Python 2.7.10 but I am porting it over to 3.4.4. The current issue with the code is a syntax error indicated at the line:

lx = [15,18,2,36,12,78,5,6,9]

but I'm not seeing much issue there. The full code is:

def obtain_rgb(image):
x1=600                                                  #black/white scale x coordinate
x2=1100                                                 #color scale x coordinate
y1=2300                                                 #black/white scale y starting coordinate
y2=250                                                  #black/white scale y ending coordinate
y3=900                                                  #color scale y ending coordinate
s1=(y1-y2)/13                                           #black/white scale step
s2=(y1-y3)/6                                            #color scale step 
pix=image.load()
ref1=pix[x1,y1]                                         #read ref1
ref2=pix[x1,y1-2*s1]                                    #read ref2
ref3=pix[x1,y1-3*s1]                                    #read ref3
ref4=pix[x1,y1-4*s1]                                    #read ref4
ref5=pix[x1,y1-5*s1]                                    #read ref5
ref6=pix[x1,y1-6*s1]                                    
ref7=pix[x1,y1-7*s1]                                    
ref8=pix[x1,y1-8*s1]                                    
ref9=pix[x1,y1-9*s1]                                    
ref10=pix[x1,y1-10*s1]                                      
ref11=pix[x1,y1-11*s1]                                  
ref12=pix[x1,y1-12*s1]                                  
ref13=pix[x1,y2]                                      
ref14=pix[x1,y1-s2]                                 
ref15=pix[x1,y1-2*s2]                                   
ref16=pix[x1,y1-3*s2]                                   
ref17=pix[x1,y1-4*s2]                                   
ref18=pix[x1,y1-5*s2]                                   
ref19=pix[x1,y1-6*s2]                                   
ref20=pix[x1,y3]                                      
try:
    with open('Informationen.csv', 'wb') as myfile:
            writer = csv.writer(myfile, dialect='excel')
            writer.writerow(ref1)                               #write ref1 to file
            writer.writerow(ref2)
            writer.writerow(ref3)
            writer.writerow(ref4)
            writer.writerow(ref5)
            writer.writerow(ref6)
            writer.writerow(ref7)
            writer.writerow(ref8)
            writer.writerow(ref9)
            writer.writerow(ref10)
            writer.writerow(ref11)
            writer.writerow(ref12)
            writer.writerow(ref13)
            writer.writerow(ref14)
            writer.writerow(ref15)
            writer.writerow(ref16)
            writer.writerow(ref17)
            writer.writerow(ref18)
            writer.writerow(ref19)
            writer.writerow(ref20)
lx = [15,18,2,36,12,78,5,6,9]   
print sum(lx)/len(lx)               #average r/g/b value from sample
except IOError as ioe:
        print('Error: ' + str(ioe))
s=(ref1,ref2)
s.split(",")
s.split(",")[1]

Any help would be greatly appreciated. Thanks.

Upvotes: 0

Views: 77

Answers (2)

Jonathan Hartley
Jonathan Hartley

Reputation: 16034

  • The lines:

    lx = [15,18,2,36,12,78,5,6,9]   
    print sum(lx)/len(lx)               #average r/g/b value from sample
    

    are inside the try...except block, so need to be indented.

  • All the code inside the function (everything after the 'def' line) should be indented.

  • To work in Python3, the 'print' statement will need parens adding, like:

    print(sum(lx)/len(lx))
    
  • This isn't a syntax error, but: Code using '/' to divide, like:

    s1=(y1-y2)/13
    s2=(y1-y3)/6
    

    will set s1 and s2 to a rounded-down integer in Python2, whereas Python3 will promote the result to a float if required, and give the 'exact' floating point representation of the division, without rounding down.

Upvotes: 0

zoubida13
zoubida13

Reputation: 1759

If you are using python 3 you should use parenthesis around your print statement :

print (sum(lx)/len(lx))

Moreover, you need to indent your code properly, after each line ending with ":" you need an indent block to follow.

Upvotes: 1

Related Questions