0

I wrote a very long "if statement" script but something doesn't work. It returns several Indentation errors but I cannot understand where...could be something else?

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import processing
#define layer
layer_sezcens=QgsVectorLayer(r'C:\Users\federica\Desktop\POLIMI\DOTTORATO\GIS MAPPING\Maggiolina_map\sezcens2.shx',"sezcens2","ogr")
#add new field
layer_sezcens.startEditing()
field_mainAge=layer_sezcens.dataProvider().addAttributes([QgsField("MainAge",QVariant.Int)])
layer_sezcens.commitChanges()
#modify new field
layer_sezcens.startEditing()
field_mainAge=layer_sezcens.dataProvider().fieldNameIndex('MainAge')
field_minDiffer=layer_sezcens.dataProvider().fieldNameIndex('MinDiffer')
field_FlatsClass= layer_sezcens.dataProvider().fieldNameIndex('maxFlatsC')
print field_mainAge,field_minDiffer,field_FlatsClass
for feat in layer_sezcens.getFeatures():
        if feat['E3']==NULL:
        expr_mainAge=0
    else:
        if feat('MinDiffer')>0: 
            if feat['MinDiffer']==feat['MaxFlatsC']-feat['E8']:
                expr_mainAge=1
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E9']:
                expr_mainAge=2
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E10']:
                expr_mainAge=3
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E11']:
                expr_mainAge=4
            elif feat['MinScarto']==feat['MaxFlatsC']-feat['E12']:
                expr_mainAge=5
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E13']:
                expr_mainAge=6
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E14']:
                expr_mainAge=7
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E15']:
                expr_mainAge=8
            elif feat['MinDiffer']==feat['MaxFlatsC']-feat['E16']:
                expr_mainAge=9
        elif feat('MinDiffer')=0: 
            list_mainAge=[feat['E8'],feat['E9'],feat['E10'],feat['E11'],feat['E12'],feat['E13'],feat['E14'],feat['E15'],feat['E16']]
            max_AgeList=max(list_mainAge)
            if feat['E8']=max_AgeList:
                expr_mainAge= 1
            elif feat['E9']=max_AgeList:
                expr_mainAge=2
            elif feat['E10']=max_AgeList:
                expr_mainAge=3
            elif feat['E11']=max_AgeList:
                expr_mainAge=4
            elif feat['E12']=max_AgeList:
                expr_mainAge=5
            elif feat['E13']=max_AgeList:
                expr_mainAge=6
            elif feat['E14']=max_AgeList:
                expr_mainAge=7
            elif feat['E15']=max_AgeList:
                expr_mainAge=8
            elif feat['E16']=max_AgeList:
                expr_mainAge=9
    print expr_mainAge
    layer_sezcens.changeAttributeValue(feat.id(), field_mainAge, expr_mainAge)
layer_sezcens.commitChanges()
  • There are 8 space before the if when it should be only 4 – underdark Aug 26 '17 at 11:09
  • @underdark thanks but which if clause do yu refer? – FedericaZ Aug 26 '17 at 12:01
  • Providing the exact error would be useful -- it would tell the exact line on which the error occurred. – Vince Aug 26 '17 at 12:08
  • @FedericaZ the first – underdark Aug 26 '17 at 13:44
1

Your first if is too indented and should be written that way:

for feat in layer_sezcens.getFeatures():
    if feat['E3']==NULL:
        expr_mainAge=0

I don't see other errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.