Skip to content

Instantly share code, notes, and snippets.

@cedrickvstheworld
Last active June 12, 2018 09:03
Show Gist options
  • Save cedrickvstheworld/fb9a1b12cd10bf17f8e12be570f0bac3 to your computer and use it in GitHub Desktop.
Save cedrickvstheworld/fb9a1b12cd10bf17f8e12be570f0bac3 to your computer and use it in GitHub Desktop.
whole number to roman numerals
def whtornb(j):
oned = {'1': 'I', '2': 'II', '3': 'III', '4': 'IV', '5': 'V',
'6': 'VI', '7': 'VII', '8': 'VIII', '9': 'IX', '0': ''}
twod = {'1': 'X', '2': 'XX', '3': 'XXX', '4': 'XL', '5': 'L',
'6': 'LX', '7': 'LXX', '8': 'LXXX', '9': 'XC', '0': ''}
threed = {'1': 'C', '2': 'CC', '3': 'CCC', '4': 'CD', '5': 'D',
'6': 'DC', '7': 'DCC', '8': 'DCCC', '9': 'CM', '0': ''}
fourd = {'1': 'M', '2': 'MM', '3': 'MMM', '4': 'MṼ', '5': 'Ṽ',
'6': 'ṼM', '7': 'VMM', '8': 'ṼMMM', '9': 'MẌ', '0': ''}
conv = [oned, twod, threed, fourd]
y = str(j)
xj = len(y)
romannum = ''
try:
for i in range(xj):
key = y[-(i + 1)]
zz = str(conv[i][key])
romannum = zz + romannum
return romannum
except IndexError:
print('out of range')
except():
print('Invalid Arguments')
x = input('Enter a number(0-1000): ')
print(whtornb(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment