| | |
Decimal to Roman Numeral
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
I need to convert a number (in decimal form) that is between 1 to 4999 to Roman Numeral Form. However, though the code I have is kinda working, its only outputting the thousand digit, not anything less or any part thats less.
if I input 1994, I get M instead of the MCMXCIV I should be getting. Any corrections and explanations to my code?
Thanks
Python Syntax (Toggle Plain Text)
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 a number (1 to 4999) in decimal form: "))
if I input 1994, I get M instead of the MCMXCIV I should be getting. Any corrections and explanations to my code?
Thanks
Here is a site where they do just what you are doing, the first bit talks about unit testing, just ignore that and keep going on, it shows you how to make a roman numeral converter that is almost impossible to break:
http://www.diveintopython.org/unit_testing/index.html
http://www.diveintopython.org/unit_testing/index.html
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
Your return statement is inside your for loop so it's exiting on the first iteration.... if you knock it back an indentation level it'll work:
python Syntax (Toggle Plain Text)
>>> 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 a number (1 to 4999) in decimal form: ")) Enter a number (1 to 4999) in decimal form: 1942 MCMXLII >>> print int2roman(input("Enter a number (1 to 4999) in decimal form: ")) Enter a number (1 to 4999) in decimal form: 1994 MCMXCIV
•
•
•
•
I need to convert a number (in decimal form) that is between 1 to 4999 to Roman Numeral Form. However, though the code I have is kinda working, its only outputting the thousand digit, not anything less or any part thats less.
Python Syntax (Toggle Plain Text)
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 a number (1 to 4999) in decimal form: "))
if I input 1994, I get M instead of the MCMXCIV I should be getting. Any corrections and explanations to my code?
Thanks
python Syntax (Toggle Plain Text)
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 a number (1 to 4999) in decimal form: "))
Last edited by sneekula; Feb 27th, 2009 at 9:28 am. Reason: code
No one died when Clinton lied.
![]() |
Similar Threads
- roman numerals (C++)
- roman numeral calculator problems (C++)
- C++ Questions (C++)
- HomeWork help with classes. Plz. (C++)
- Novice asking for help in Learning C++ (C++)
Other Threads in the Python Forum
- Previous Thread: What are the .apy files in Python
- Next Thread: What's with the None?
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web windows wx.wizard wxpython xlwt






