Hey,just a few notes on that:
- Make sure your data types are correct. If the input is field actually a string, you will need to put quotes around the numbers or convert Class to an integer before the conditionals (also handy if you need to do comparative queries, < or >, for example):
if Class == '1' ... and so on...
or
def func(Class_input):
Class = int(Class_input)
if Class == 1 ... and so on...
if Class == '1' or Class == '2':
- use brackets for multiple conditional statements (not always necessary, but a good idea):
if (Class == '1') or (Class == '2'):
- else/elif at the end is up to the programmer, but note that else doesn't take a conditional, it just does its thing if none of the if/elif statements worked. Generally it is a good idea to have an else at the end, as it will help you identify any messy stuff with your data. For example, if 6 is passed to your code it will fail. What I often do is put else: return None at the end, which will make the field NULL... This does, however depend upon on your application, you could, for example, return 'A50'...
Codeblock:
def func(Class):
if (Class == '1') or (Class == '2'):
return "A15"
elif Class == '3':
return "A20"
elif Class == '4':
return "A30"
elif Class == '5':
return "A40"
else:
return None
Expression:
func(!theclassfield!)