| | |
Roman Numerals (Python)
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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.
# convert an integer to a roman numeral # keep it reasonable since from 5000 on special characters are used # see also: http://en.wikipedia.org/wiki/Roman_numerals # tested with Python24 vegaseat 25jan2007 def int2roman(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 int2roman(input("Enter an integer (1 to 4999): ")) """ Enter an integer (1 to 4999): 2007 MMVII """
0
•
•
•
•
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
•
•
•
•
I just tested it with python 2.6 and it works. For python 3.1, I had to change the last statement to
and it works.
python Syntax (Toggle Plain Text)
print(int2roman(int(input("Enter an integer (1 to 4999): "))))
0
•
•
•
•
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
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
•
•
•
•
@@pythonuser18 For reference, I got it working, with python 2.6 and this last line:
python Syntax (Toggle Plain Text)
print DecToRom(int(raw_input("Decimal number: ")))
1
•
•
•
•
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.
Python Syntax (Toggle Plain Text)
roman_to_decimal = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, \ 'D': 500, 'M': 1000 } roman = raw_input("Enter the roman numeral to convert to arabic: ").upper() converted = True arabic = 0 for n in range(0, len(roman)-1): this_char = roman[n] next_char = roman[n+1] if (this_char in roman_to_decimal) and \ (next_char in roman_to_decimal): this_number = roman_to_decimal[this_char] next_number = roman_to_decimal[next_char] print "converting %s to %d....." % (this_char, this_number), if this_number < next_number: arabic -= this_number else: arabic += this_number print "Total now =", arabic else: print "\nthis chr (%s) or next chr (%s) is not a valid roman numeral" \ % (this_char, next_char) converted = False break if converted: ## add last roman numeral arabic += roman_to_decimal[roman[len(roman)-1]] print "\nThe roman numeral", roman, "is equal to",arabic
Similar Threads
- Need a little help...(Conversion of Arabic Numerals to Roman Numerals(vice-versa)).. (Visual Basic 4 / 5 / 6)
- Converting Numbers to Roman Numerals (Pascal and Delphi)
- Help with converting Roman numerals to Integer values (Python)
- roman numerals (C++)
- help with program to convert integers to roman numerals (Java)
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython



