Help! I think I am missing something that must be so obvious but I cannot figure out what I am doing wrong. Here's some background:
I have one short integer field (MajAspNum) that has values 1-10. I have another text field (Direction) that needs to be filled. I am trying to use the Field Calculator to calculate it but am not succeeding in any way. I should know what I'm doing but I just can't seem to make it work.
Pre-logic Script Code:
def des(num):
if num = 1:
return "Flat"
elif num = 2:
return "North"
elif num = 3:
return "Northeast"
elif num = 4:
return "East"
elif num = 5:
return "Southeast"
elif num = 6:
return "South"
elif num = 7:
return "Southwest"
elif num = 8:
return "West"
elif num = 9:
return "Northwest"
elif num = 10:
return "North"
else:
return "N/A"
Direction =
des (!MajAspNum!)
It gives me a GeoProcessing error of invalid syntax on line 2. I have tried just selecting by attributes in the attributes table and then using field calculator more simply on just those but that gives me a very weird error that literally makes no sense. I'm hoping my error is a really obvious fix to someone out there!
Answer
A switch statement is ideal for what you want to do. Since Python doesn't support switch statements, you can use dictionary mapping to accomplish the same thing.
def des(num):
the_dict = {1: 'Flat',
2: 'North',
3: 'Northeast',
4: 'East',
5: 'Southeast',
6: 'South',
7: 'Southwest',
8: 'West',
9: 'Northwest',
10: 'North'
}
return the_dict.get(num, 'N/A')
No comments:
Post a Comment