1

I am trying to convert numbers (up to 30) to roman numerals. Using a script for another field calculator function as a template, I created this script that doesn't want to work. Any help would be greatly appreciated!

def NumberToRomanNumeral ( !CONCESSION! ):
 if CONCESSION == "1":
  newstr = "I"
elif CONCESSION == 2:
 newstr = "II"
elif CONCESSION == 3:
 newstr = "III"    
elif CONCESSION == 4:
 newstr = "IV"    
elif CONCESSION == 5:
  newstr = "V"  
elif CONCESSION == 6:
 newstr = "VI"  
elif CONCESSION == 7:
 newstr = "VII" 
elif CONCESSION == 8:
 newstr = "VIII"  
elif CONCESSION == 9:
 newstr = "IX"  
elif CONCESSION == 10:
 newstr = "X" 
elif CONCESSION == 11:
 newstr = "XI"
elif CONCESSION == 12:
 newstr = "XII"
elif CONCESSION == 13:
 newstr = "XIII"
elif CONCESSION == 14:
 newstr = "XIV"
elif CONCESSION == 15:
 newstr = "XV"
elif CONCESSION == 16:
 newstr = "XVI"
elif CONCESSION == 17:
 newstr = "XVII"
elif CONCESSION == 18:
 newstr = "XVIII"
elif CONCESSION == 19:
 newstr = "XIX"
elif CONCESSION == 20:
 newstr = "XX"
elif CONCESSION == 21:
 newstr = "XXI"
elif CONCESSION == 22:
 newstr = "XXII"
elif CONCESSION == 23:
 newstr = "XXIII"
elif CONCESSION == 24:
 newstr = "XXIV"
elif CONCESSION == 25:
 newstr = "XXV"
elif CONCESSION == 26:
 newstr = "XXVI"
elif CONCESSION == 27:
 newstr = "XXVII"
elif CONCESSION == 28:
 newstr = "XXVIII"
elif CONCESSION == 29:
 newstr = "XXIX"
elif CONCESSION == 30:
 newstr = "XXX"


else:
   newstr = ""
return newstr
  • 1
    Is there a particular error message that you're getting? –  Jan 23 '12 at 16:39
  • 6
    There are some existing scripts out there to do the conversion. For example, take a look at http://code.activestate.com/recipes/81611-roman-numerals/ – kenbuja Jan 23 '12 at 16:40
  • 2
    Is your input numeric or string? Your first if statement uses quotes while the rest don't. – kenbuja Jan 23 '12 at 16:43
  • I'd say check your tabs as well as the data types as kenbuja notes. you could coerce into the appropriate type via something like elif int(CONCESSION) == 28: - I'd also second the use of an existing script for that. – Nick Jan 23 '12 at 19:22
  • Sorry this doesn't answer your question directly, but one thing you might consider in the future for doing this kind of conversion are Dictionaries, which are a built-in Python datatype that are a much easier alternative to long elif lists. See: http://docs.python.org/tutorial/datastructures.html. Saves some typing, and probably easier to find issues in the code. But my guess is your problem what kenbuja brought up - if your input is a string, you need to make sure all of your statements are checking for string values. – Jason Baker Jan 24 '12 at 01:13
  • I think you need... Expression: NumberToRomanNumeral(!CONCESSION!) then Code Block starting... def NumberToRomanNumeral(CONCESSION): then decide if CONCESSION is number or text (needs quotes) for the if statements then instead of lines like newstr = "I" use return "I" – PolyGeo Jan 24 '12 at 03:10
  • Got it to work! The error was on the first line. Instead of ( CONCESSION ) it was input as ( !CONCESSION! ). Thanks for the help! –  Jan 24 '12 at 16:42
  • Why has no one criticised the horror of this code snippet using the fact that it should be more procedural? – Xavier Ho Jan 25 '12 at 00:03

1 Answers1

1

Exclamation marks are in wrong place - it should be

Expression:

NumberToRomanNumeral(!CONCESSION!)

Code Block: starting...

def NumberToRomanNumeral(CONCESSION):
PolyGeo
  • 1,340
  • 3
  • 26
  • 59