Roman Numerals (Python)

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Jan 26th, 2007, 3:14 am |
0
Let Python do the work for you and figure out the roman numerals for a given integer. I have to ask you to keep the integer values somewhat reasonably low (from 1 to 4999), since roman numerals from 5000 on use characters with an overline and most PCs don't have those in the character set.
Last edited by vegaseat; Oct 17th, 2009 at 4:25 pm.
Quick reply to this message  
Python Syntax
  1. # convert an integer to a roman numeral
  2. # keep it reasonable since from 5000 on special characters are used
  3. # see also: http://en.wikipedia.org/wiki/Roman_numerals
  4. # tested with Python24 vegaseat 25jan2007
  5.  
  6. def int2roman(number):
  7. numerals = { 1 : "I", 4 : "IV", 5 : "V", 9 : "IX", 10 : "X", 40 : "XL",
  8. 50 : "L", 90 : "XC", 100 : "C", 400 : "CD", 500 : "D", 900 : "CM", 1000 : "M" }
  9. result = ""
  10. for value, numeral in sorted(numerals.items(), reverse=True):
  11. while number >= value:
  12. result += numeral
  13. number -= value
  14. return result
  15.  
  16. print int2roman(input("Enter an integer (1 to 4999): "))
  17.  
  18. """
  19. Enter an integer (1 to 4999): 2007
  20. MMVII
  21. """
0
pythonuser18 pythonuser18 is offline Offline | Oct 15th, 2009
i'm just playing around with python and decided to try this code out but keep getting a syntax error pointing to the colon after the 1 in,
 
0
Gribouillis Gribouillis is offline Offline | Oct 15th, 2009
I just tested it with python 2.6 and it works. For python 3.1, I had to change the last statement to
  1. print(int2roman(int(input("Enter an integer (1 to 4999): "))))
and it works.
 
0
vegaseat vegaseat is offline Offline | Oct 15th, 2009
@pythonuser18
you used () instead of {}
a dictionary container needs the curly braces.
 
0
pythonuser18 pythonuser18 is offline Offline | Oct 16th, 2009
def DecToRom (number) :
numerals={1: "I", 4: "IV", 5: "V", 9: "IX", 10: "X", 40: "XL", 50: "L", 90: "XC", 100: "C", 400: "CD", 500: "D", 900: "CM", 1000: "M"}
result=""
for value, numeral in sorted(numerals.items(), reverse=True):
while number >= value:
result += numeral
number -= value
return result
print DecToRom(raw_input(“Decimal number: “))

could someone tell me why this isn't working? It says Syntax Error: invalid syntax

I know the indents aren't formatted correctly on this post, but they are as they are suppose to be in python
Last edited by pythonuser18; Oct 16th, 2009 at 12:57 am.
 
1
Gribouillis Gribouillis is offline Offline | Oct 16th, 2009
@@pythonuser18 For reference, I got it working, with python 2.6 and this last line:
  1. print DecToRom(int(raw_input("Decimal number: ")))
 
1
woooee woooee is offline Offline | Oct 16th, 2009
Roman to integer. Note that it will take something like XXIXV and convert it. If someone already has a routine to check for valid input, please post it. And I'm afraid we've just eliminated one standard homework question. Oh well, nothing lasts forever.
  1. roman_to_decimal = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, \
  2. 'D': 500, 'M': 1000 }
  3.  
  4. roman = raw_input("Enter the roman numeral to convert to arabic: ").upper()
  5.  
  6. converted = True
  7. arabic = 0
  8. for n in range(0, len(roman)-1):
  9. this_char = roman[n]
  10. next_char = roman[n+1]
  11. if (this_char in roman_to_decimal) and \
  12. (next_char in roman_to_decimal):
  13.  
  14. this_number = roman_to_decimal[this_char]
  15. next_number = roman_to_decimal[next_char]
  16. print "converting %s to %d....." % (this_char, this_number),
  17. if this_number < next_number:
  18. arabic -= this_number
  19. else:
  20. arabic += this_number
  21. print "Total now =", arabic
  22.  
  23. else:
  24. print "\nthis chr (%s) or next chr (%s) is not a valid roman numeral" \
  25. % (this_char, next_char)
  26. converted = False
  27. break
  28.  
  29. if converted:
  30. ## add last roman numeral
  31. arabic += roman_to_decimal[roman[len(roman)-1]]
  32.  
  33. print "\nThe roman numeral", roman, "is equal to",arabic
 
 

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC