Aug-09-2020, 04:31 AM
(This post was last modified: Aug-09-2020, 01:42 PM by deanhystad.)
Your Roman Numerals are in the wrong order. You provided a great example for how to solve the problem, then you wrote code that completely ignores the example.
In your example the first thing you did was find out how many 1000's there are in the number. For each 1000 you subtracted 1000 from the number and added 'M' to the Roman Numeral. Then you repeated this for the next lower value numeral (900 and CM). Repeat for every roman numeral in order of decreasing value until the remaining number is zero.
In your program you do the opposite. You start with the lowest value numeral work your way up. You know how to do this, don't freeze up just because you are now writing the algorithm in Python.
Maybe that is what you need, a written algorithm that can direct your coding.
In your example the first thing you did was find out how many 1000's there are in the number. For each 1000 you subtracted 1000 from the number and added 'M' to the Roman Numeral. Then you repeated this for the next lower value numeral (900 and CM). Repeat for every roman numeral in order of decreasing value until the remaining number is zero.
In your program you do the opposite. You start with the lowest value numeral work your way up. You know how to do this, don't freeze up just because you are now writing the algorithm in Python.
Maybe that is what you need, a written algorithm that can direct your coding.
def roman_numeral(number): # Map of roman numerals and values ordered in decreasing value. NUMERALS = {1000:'M', 900:'CM', 500:'D', 400:'CD', 100:'C', 90:'XC', 50:'L', 40:'XL', 10:'X', 9:'IX', 5:'V', 4:'IV', 1:'I'} # Start with an empty string retval = '' # Starting with the highest value, loop through all numerals ...: # While number is >= value, subtract value from number and add numeral to retval ... return retvalNext time write the comments yourself. If you have a hard time writing the comments it means you don't understand the problem. If you cannot write a good description of the algorithm you cannot write a program.