0

I'm trying to create street range labels rounded to the nearest 100 unless a street range is less than 100 I'm using "50". I have a field l_refaddr that is a string with number ranges for a street segment. If a string value has less than 3 characters i want the expression to return "50", if the length is 3, then return first character and concatenate with "00", if length is 4, first two characters and "00", if legth is 5, first three characters and "00". Here's what I wrote in the expression builder:

    def FindLabel ( [l_refaddr] ):
      if len( [l_refaddr]) < 3:
        [l_refaddr] = "50"
      elif len( [l_refaddr]) == 3:
        [l_refaddr] =  [l_refaddr][0]+"00"
      elif len( [l_refaddr]) == 4:
        [l_refaddr] =  [l_refaddr][:2]+"00"
      else:
    [l_refaddr] = [l_refaddr][:3]+"00"
    return [l_refaddr]

And this is the error I'm getting:

enter image description here

Any ideas what I'm missing?

4
  • 1
    You can't set your field in a label expression, try using a variable like MyLabel = "50" then return MyLabel. That error sounds though like you've got a Null value in your table, try if [l_refaddr] == None: MyLabel = '' else: to enclose that block, indexing a None is probably what's causing that error message. Jul 30, 2018 at 22:20
  • @MichaelStimson That did the trick. I corrected the code in the post. Put it down as an answer and I'll check it off. Thanks!
    – geoJshaun
    Jul 30, 2018 at 23:05
  • 1
    Please always provide errors as text in preference, or in addition, to as pictures so that they are available to future searches by others with the same problem.
    – PolyGeo
    Jul 30, 2018 at 23:14
  • 3
    You've already got the answer in your question, please answer your own question with the updated code. Jul 31, 2018 at 0:49

1 Answer 1

1

Michael Stimson pointed me in the right direction. I needed to use a variable name for assigning new values instead of the field name. Here is the corrected code:

def FindLabel ( [l_refaddr] ):
  if  [l_refaddr] == None:
    MyLabel = ''
  elif len( [l_refaddr]) < 3:
    MyLabel = "50"
  elif len( [l_refaddr]) == 3:
    MyLabel =  [l_refaddr][0]+"00"
  elif len( [l_refaddr]) == 4:
    MyLabel =  [l_refaddr][:2]+"00"
  else:
    MyLabel = [l_refaddr][:3]+"00"
  return MyLabel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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