python syntax error

809
3
Jump to solution
12-13-2016 11:50 AM
MatthewUpchurch1
New Contributor II

I'm having a bit of trouble figuring out what is wrong with this script. I am trying to tell it "if there is a direction in the pdir/sdir field, concatenate the string into the field, otherwise just print the sname + typ". It says the syntax error is on line 3. Thanks.

def reclass(pdir,sdir,typ,sname):
    if (pdir == 'N'):
        return pdir + ' ' + sname + ' ' typ
    elif (pdir == 'S'):
        return pdir + ' ' + sname + ' ' typ
    elif (pdir == 'E'):
        return pdir + ' ' + sname + ' ' typ
    elif (pdir == 'W'):
        return pdir + ' ' + sname + ' ' typ
    elif (sdir == 'N'):
        return sname + ' ' typ + ' ' + sdir
    elif (sdir == 'S'):
        return sname + ' ' typ + ' ' + sdir
    elif (sdir == 'E'):
        return sname + ' ' typ + ' ' + sdir
    elif (sdir == 'W'):
        return sname + ' ' typ + ' ' + sdir
    else:
        return sname + ' ' + typ

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

return pdir + ' ' + sname + ' ' typ  

you are missing a + before typ  and if typ is numeric it should be str(typ)

better

"{} {} {}".format(pdir, sname, typ)

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

return pdir + ' ' + sname + ' ' typ  

you are missing a + before typ  and if typ is numeric it should be str(typ)

better

"{} {} {}".format(pdir, sname, typ)
MatthewUpchurch1
New Contributor II

Thanks Dan Patterson! Amateur mistake lol. How would I use the .format() in the field calculator though?

0 Kudos
DanPatterson_Retired
MVP Emeritus

just replace all your 

return ... your lines where you will forget the plus or space    ..... with

return  ...my line

return  .... modify my line 1 to reflect your new order for N, S, E and W it

ie sname, typ, sdir

my format line replaces the { } with the contents of the values in the format side. since they are in order they will fill those brackets with the values... Note, that I put a space inbetween them... you could use commas or whatever in between them.  The trick is  the values are converted to strings automatically so there is no need for str ing anything.  and it will return a string as output