Skip to content

Instantly share code, notes, and snippets.

@aldente39
Created January 4, 2012 11:21
Show Gist options
  • Save aldente39/1559631 to your computer and use it in GitHub Desktop.
Save aldente39/1559631 to your computer and use it in GitHub Desktop.
roman_numerals
def main():
while True:
n = input()
if 0 < n < 10000:
break
print 'Error: input 1 ~ 9999'
s = str(n)
c = ''
try:
if s[-1] == '4':
c += 'IV'
elif s[-1] == '9':
c += 'IX'
else:
if int(s[-1]) >= 5:
c += 'V' + ((int(s[-1]) - 5) * 'I')
else:
c += int(s[-1]) * 'I'
if s[-2] == '4':
c = 'XL' + c
elif s[-2] == '9':
c = 'XC' + c
else:
if int(s[-2]) >= 5:
c = 'L' + ((int(s[-2]) - 5) * 'X') + c
else:
c = int(s[-2]) * 'X' + c
if s[-3] == '4':
c = 'CD' + c
elif s[-3] == '9':
c = 'CM' + c
else:
if int(s[-3]) >= 5:
c = 'D' + ((int(s[-3]) - 5) * 'C') + c
else:
c = int(s[-3]) * 'C' + c
c = int(s[-4]) * 'M' + c
except IndexError:
pass
print c
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment