Decimal to Roman Numeral

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 5
Reputation: curiouskitten is an unknown quantity at this point 
Solved Threads: 0
curiouskitten curiouskitten is offline Offline
Newbie Poster

Decimal to Roman Numeral

 
0
  #1
Feb 27th, 2009
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.
  1. def int2roman(number):
  2. numerals={1:"I", 4:"IV", 5:"V", 9: "IX", 10:"X", 40:"XL", 50:"L",
  3. 90:"XC", 100:"C", 400:"CD", 500:"D", 900:"CM", 1000:"M"}
  4. result=""
  5. for value, numeral in sorted(numerals.items(), reverse=True):
  6. while number >= value:
  7. result += numeral
  8. number -= value
  9. return result
  10. 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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 909
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Decimal to Roman Numeral

 
0
  #2
Feb 27th, 2009
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
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Decimal to Roman Numeral

 
0
  #3
Feb 27th, 2009
Originally Posted by curiouskitten View Post
Any corrections and explanations to my code?
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:
  1. >>> def int2roman(number):
  2. ... numerals={1:"I", 4:"IV", 5:"V", 9: "IX", 10:"X", 40:"XL", 50:"L",
  3. ... 90:"XC", 100:"C", 400:"CD", 500:"D", 900:"CM", 1000:"M"}
  4. ... result=""
  5. ... for value, numeral in sorted(numerals.items(), reverse=True):
  6. ... while number >= value:
  7. ... result += numeral
  8. ... number -= value
  9. ... return result
  10. ...
  11. >>> print int2roman(input("Enter a number (1 to 4999) in decimal form: "))
  12. Enter a number (1 to 4999) in decimal form: 1942
  13. MCMXLII
  14. >>> print int2roman(input("Enter a number (1 to 4999) in decimal form: "))
  15. Enter a number (1 to 4999) in decimal form: 1994
  16. MCMXCIV
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Decimal to Roman Numeral

 
0
  #4
Feb 27th, 2009
Originally Posted by curiouskitten View Post
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.
  1. def int2roman(number):
  2. numerals={1:"I", 4:"IV", 5:"V", 9: "IX", 10:"X", 40:"XL", 50:"L",
  3. 90:"XC", 100:"C", 400:"CD", 500:"D", 900:"CM", 1000:"M"}
  4. result=""
  5. for value, numeral in sorted(numerals.items(), reverse=True):
  6. while number >= value:
  7. result += numeral
  8. number -= value
  9. return result
  10. 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
You are almost there, your return statement needs to be outside the for loop.
  1. def int2roman(number):
  2. numerals={1:"I", 4:"IV", 5:"V", 9: "IX", 10:"X", 40:"XL", 50:"L",
  3. 90:"XC", 100:"C", 400:"CD", 500:"D", 900:"CM", 1000:"M"}
  4. result=""
  5. for value, numeral in sorted(numerals.items(), reverse=True):
  6. while number >= value:
  7. result += numeral
  8. number -= value
  9. return result
  10.  
  11. print int2roman(input("Enter a number (1 to 4999) in decimal form: "))
Sorry, didn't see Pen's early response.
Last edited by sneekula; Feb 27th, 2009 at 9:28 am. Reason: code
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC