Select to view content in your preferred language

Python Help needed

2307
11
Jump to solution
11-02-2017 12:09 PM
StevenFama
Occasional Contributor

I have a script that populates/updates a field based upon its content. If none of the conditions are true, I need it not to update / return <Null>. 

def Reclass(StoryHeight_Copy): 
if (StoryHeight_Copy == "1A"): 
return "1.25" 
elif (StoryHeight_Copy == "1H"): 
return "1.50" 
elif (StoryHeight_Copy == "1T"): 
return "1.75" 
elif (StoryHeight_Copy == "2A"): 
return "2.25"
elif (StoryHeight_Copy == "2H"): 
return "2.50" 
elif (StoryHeight_Copy == "2T"): 
return "2.75" 
elif (StoryHeight_Copy == "3A"): 
return "3.25"
elif (StoryHeight_Copy == "3H"): 
return "3.50" 
elif (StoryHeight_Copy == "3T"): 
return "3.75"

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

PEP 8 ... would help as well and going forward in Python 3.5+, PEP484... only a few covered here to improve readability.

Code Formatting... the basics ++ as well as others is useful to provide clean code with consistent indentation for the novice pythonista

def Reclass(StoryHeight_Copy):
    d = {"1A": "1.25", "1H": "1.5", "1T": "1.75"}
    if StoryHeight_Copy in d:
        return d[StoryHeight_Copy]
    return None
XanderBakker
Esri Esteemed Contributor

I the examples shared by carl anderson  and Dan Patterson there should be an else statement that returns the original value in case it is not in the dictionary as curtvprice  showed earlier.