Simple Roman Number Converter

Hello and welcome my dearest guest, this is the first post on this blog and I would like to say thank you for coming by. Anyway, I love programming, software, and technology … So, I will be posting mostly all about these.

We shall start with simple and useful a small python program “Roman Number Converter” (i used python 3).

Let’s start this,

  • 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 )

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'}
  • One more dictionary for previous four dictionaries. Decimal contains only 1 digit,  Tens contains 2 digits, and so on…
NumLength = { 1 : decimals, 2 : tens, 3 : hundreds, 4 : thousands}

And then, we will write a function to handle query (input) number and our function,

  1. Convert input number into a string and make a list.
  2. Loop through each digit and collect associate Roman number.

The trick is to fetch a value from the NumLength dictionary with list length (that make sure to collect a Roman number from the correct dictionary). So, we have to slice the first digit for each time until the loop is complete.

def RomanNumberConverter(number):
        # convert string list
	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);

And finally, we call our function to see the result.

while True:
    userInput = input("Input Number: ");
    if not userInput:
        break;
    print("Roman Number: %s\n" %RomanNumberConverter(userInput));

It works like a charm but of course, we haven’t written any errors handler yet. The following is an output result from our simple program. If you want to terminate the program just press ENTER.

Input Number: 2935
Roman Number: MMCMXXXV

Input Number: 1990
Roman Number: MCMXC

Input Number: 365
Roman Number: CCCLXV

Input Number:
>>>

Thanks for your time, have a great day.
Here, you can get RomanNumber.py

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.