0

I am new to python and was trying to populate a decade field based on an if else statement on another field that contains parcel years.

The code block is below:

def ifBlock(Parcel_Year_Built, Decade_Grouping):
    if (Parcel_Year_Built < '1960' and > '0'):
        return "59 and earlier"
    elif (Parcel_Year_Built >= '1960' and < '1970'):
        return "60s"
    elif (Parcel_Year_Built >= '1970' and < '1980'):
        return "70s"
    elif (Parcel_Year_Built >= '1980' and < '1991'):
        return "80s"
    elif (Parcel_Year_Built >= '1991'):
        return "Post Ban"```

and the code is:

ifBlock(!Parcel_Year_Built!,!Decade_Grouping!)

The error I get is:

  File "<string>", line 2
    if (Parcel_Year_Built < '1960' and > '0'):
                                           ^
SyntaxError: invalid syntax
3
  • 1
    second condition of the if statement should include your variable. For example, if (Parcel_Year_Built < '1960' and Parcel_Year_Built > '0'): return "59 and earlier"
    – foxhq
    Commented Sep 28, 2023 at 14:12
  • Ah. Thank you! That worked. I really appreciate it.
    – Androoo
    Commented Sep 28, 2023 at 14:22
  • 1
    It is very unwise to attempt integer comparison using strings. You can get bizarre results based on character set codepoint order, not integer values.
    – Vince
    Commented Sep 28, 2023 at 15:45

1 Answer 1

1

Adjust your code with the following code block

def ifBlock(Parcel_Year_Built, Decade_Grouping):
    if (Parcel_Year_Built < '1960' and Parcel_Year_Built > '0'):
        return "59 and earlier"
    elif (Parcel_Year_Built >= '1960' and Parcel_Year_Built < '1970'):
        return "60s"
    elif (Parcel_Year_Built >= '1970' and Parcel_Year_Built < '1980'):
        return "70s"
    elif (Parcel_Year_Built >= '1980' and Parcel_Year_Built < '1991'):
        return "80s"
    elif (Parcel_Year_Built >= '1991'):
        return "Post Ban"

Your Answer

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

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