Hello and welcome, this is the first post on this blog, we shall start with simple and useful a small python program “Roman Number Converter” (python 3).
- First, we need Roman numbers
- And make python dictionaries for decimals, tens, hundreds and thousands. (The largest number will be 3999. Feel free to extend as large as you want, just simply add more dictionaries )
- Another dictionary for previous four dictionaries. Decimal contains only 1 digit, Tens contains 2 digits, and so on…
decimals = { '0' : '', '1' : 'I', '2' : 'II', '3' : 'III', '4' : 'IV',
'5' : 'V', '6' : 'VI', '7' : 'VII', '8' : 'VIII', '9' :'IX'}
tens = { '0' : '', '1' :'X', '2' :'XX', '3' :'XXX', '4' :'XL', '5' :'L',
'6' :'LX', '7' :'LXX', '8' :'LXXX', '9' :'XC'}
hundreds = { '0' : '', '1' :'C', '2' :'CC', '3' :'CCC', '4' :'CD',
'5' :'D', '6' :'DC', '7' :'DCC', '8' :'DCCC', '9' :'CM'}
thousands = { '0' : '', '1' : 'M', '2' : 'MM', '3' : 'MMM'}
NumLength = { 1 : decimals, 2 : tens, 3 : hundreds, 4 : thousands}
And, a function to handle query (input) number :
- Convert input number into a string and make a list.
- Loop through each digit and collect Roman number from the each of the dictionary.
- Fetch a value from the NumLength dictionary with list length (that make sure to collect a Roman number from the correct dictionary).
def RomanNumberConverter(number):
makeList = list(str(number));
result = [];
for num in makeList:
# get value(a dictionary) from NumLength dictionary
# get roman number from this value(a dictionary)
firstNumber = NumLength[len(makeList)][num];
# remove first digit from list
makeList = makeList[1:];
result.append(firstNumber);
return ''.join(result);
while True:
userInput = input("Input Number: ");
if not userInput:
break;
print("Roman Number: %s\n" %RomanNumberConverter(userInput));
The following is an output result from our simple program. Just press Enter to terminate the program.
Input Number: 2935
Roman Number: MMCMXXXV
Input Number: 1990
Roman Number: MCMXC
Input Number: 365
Roman Number: CCCLXV
Input Number:
Complete source code : RomanNumber.py