<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python if elif always defaults to elif in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267259#M67067</link>
    <description>&lt;P&gt;Ah, I think you mean to get the length of the field and not length of the field name.&amp;nbsp; Sorry about that-&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fld = arcpy.ListFields(in_Table, in_Field)

if fld:
    if fld[0].length == 254:
        arcpy.AddWarning(in_Field + " exists and is the correct length. Yay!")
    else:
        arcpy.AddWarning(in_Field + " exists but is not the correct string length (254).")
else:
    arcpy.AddField_management(in_Table, in_Field, "TEXT", "", "", 254, in_Field, "NON_NULLABLE", "REQUIRED", "")
    arcpy.AddMessage("Created the " + in_Field + " field.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Mar 2023 19:13:50 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2023-03-13T19:13:50Z</dc:date>
    <item>
      <title>Python if elif always defaults to elif</title>
      <link>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267248#M67065</link>
      <description>&lt;P&gt;With the following I am evaluating:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Does the field exist and correct length. Print message&lt;/LI&gt;&lt;LI&gt;Does the field exist and incorrect length. Print message&lt;/LI&gt;&lt;LI&gt;Does the field not exist. Create field. Print Message&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;#Create required TRLNAME Field
try:
        in_Field = "TRLNAME"
        if len(arcpy.ListFields(in_Table,in_Field))&amp;gt;0 and len(in_Field) == 254:
                arcpy.AddWarning(in_Field+" exists and is the correct length. Yay!")
        elif len(arcpy.ListFields(in_Table,in_Field))&amp;gt;0 and len(in_Field) != 254:
                arcpy.AddWarning(in_Field+" exists but is not the correct string length (254).")
        else:
                arcpy.AddField_management(in_Table, in_Field, "TEXT", "", "", 254, in_Field, "NON_NULLABLE", "REQUIRED", "")
                arcpy.AddMessage ("Created the "+in_Field+" field.")
except arcpy.ExecuteError:
        msgs = arcpy.GetMessages(2)
        arcpy.AddError(msgs)
except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;If the field does not exist, it correctly creates it. However, if the field exists, regardless of if it's the correct length (254 or any other string length), it always defaults to the "elif" in the block and reports that the field is not the correct length.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Feel like this is a simple syntax error but I've been at it for hours and not seeing it. Help?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2023 18:49:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267248#M67065</guid>
      <dc:creator>ThomasColson</dc:creator>
      <dc:date>2023-03-13T18:49:14Z</dc:date>
    </item>
    <item>
      <title>Python if elif always defaults to elif</title>
      <link>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267246#M67069</link>
      <description>&lt;P&gt;With the following I am evaluating:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Does the field exist and correct length. Print message&lt;/LI&gt;&lt;LI&gt;Does the field exist and incorrect length. Print message&lt;/LI&gt;&lt;LI&gt;Does the field not exist. Create field. Print Message&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;#Create required TRLNAME Field
try:
        in_Field = "TRLNAME"
        if len(arcpy.ListFields(in_Table,in_Field))&amp;gt;0 and len(in_Field) == 254:
                arcpy.AddWarning(in_Field+" exists and is the correct length. Yay!")
        elif len(arcpy.ListFields(in_Table,in_Field))&amp;gt;0 and len(in_Field) != 254:
                arcpy.AddWarning(in_Field+" exists but is not the correct string length (254).")
        else:
                arcpy.AddField_management(in_Table, in_Field, "TEXT", "", "", 254, in_Field, "NON_NULLABLE", "REQUIRED", "")
                arcpy.AddMessage ("Created the "+in_Field+" field.")
except arcpy.ExecuteError:
        msgs = arcpy.GetMessages(2)
        arcpy.AddError(msgs)
except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;If the field does not exist, it correctly creates it. However, if the field exists, regardless of if it's the correct length (254 or any other string length), it always defaults to the "elif" in the block and reports that the field is not the correct length.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Feel like this is a simple syntax error but I've been at it for hours and not seeing it. Help?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2023 18:48:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267246#M67069</guid>
      <dc:creator>ThomasColson</dc:creator>
      <dc:date>2023-03-13T18:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Python if elif always defaults to elif</title>
      <link>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267259#M67067</link>
      <description>&lt;P&gt;Ah, I think you mean to get the length of the field and not length of the field name.&amp;nbsp; Sorry about that-&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fld = arcpy.ListFields(in_Table, in_Field)

if fld:
    if fld[0].length == 254:
        arcpy.AddWarning(in_Field + " exists and is the correct length. Yay!")
    else:
        arcpy.AddWarning(in_Field + " exists but is not the correct string length (254).")
else:
    arcpy.AddField_management(in_Table, in_Field, "TEXT", "", "", 254, in_Field, "NON_NULLABLE", "REQUIRED", "")
    arcpy.AddMessage("Created the " + in_Field + " field.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2023 19:13:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267259#M67067</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-03-13T19:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python if elif always defaults to elif</title>
      <link>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267267#M67068</link>
      <description>&lt;P&gt;The problem is in the second part of the if statement. You defined the "in_Field"&amp;nbsp; variable on the 3rd line and that's a string. So length of that string will always be 7 (number of letters in the&amp;nbsp;TRLNAME) and that will never change, so it never meets the criteria of being equal 254. You want to test the field properties not its name.&lt;/P&gt;&lt;P&gt;I will probably do something like this and also separate the check for field existence from the field length check.&amp;nbsp; I wrote the basic idea and you need to replace the print statement with whatever you want to do.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if len(arcpy.ListFields(in_Table, in_Field))&amp;gt;0:
    if arcpy.ListFields(in_Table, in_Field)[0].length == 254:
        print('254 should fall into this now!')
    else: 
        print('other lengths should fall into this!')
else:
    print('Create new')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if the field exists, arcpy.ListFields() will return a list and that&amp;nbsp;field (as an object) is an element of that list, so you get the first item on that list ([0]) and check the length of that.&lt;/P&gt;&lt;P&gt;Sometimes it easy to not spot the problem in your code right away! In that case, just stop, take a walk and come back a few min later you will probably see it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2023 19:25:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267267#M67068</guid>
      <dc:creator>Mahdi_Ch</dc:creator>
      <dc:date>2023-03-13T19:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Python if elif always defaults to elif</title>
      <link>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267378#M67072</link>
      <description>&lt;P&gt;Thanks this worked!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 00:28:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-elif-always-defaults-to-elif/m-p/1267378#M67072</guid>
      <dc:creator>ThomasColson</dc:creator>
      <dc:date>2023-03-14T00:28:43Z</dc:date>
    </item>
  </channel>
</rss>

