Guest User

Untitled

a guest
Nov 16th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. def int_to_roman(n):
  2. string=''
  3. symbol=['M','D','C','L','X','V','I']
  4. value = [1000,500,100,50,10,5,1]
  5. num = 10 ** (len(str(n)) - 1)
  6. quo = n // num
  7. rem = n % num
  8. if quo in [0,1,2,3]:
  9. string = string + symbol[value.index(num)] * quo
  10. elif quo in [4,5,6,7,8]:
  11. tem_str = symbol[value.index(num)] + symbol[value.index(num) - 1] + symbol[value.index(num)] * 3
  12. string = string + tem_str[(min(quo,5) - 4) : (max(quo,5) - 3)]
  13. else:
  14. string = string + symbol[value.index(num)] + symbol[value.index(num) - 2]
  15. if rem == 0:
  16. return string
  17. else:
  18. string = string + int_to_roman(rem)
  19. return string
  20.  
  21. try:
  22. n = int(input('Give natural number (written in Arabic numerals, greater than zero and less than 4000):'))
  23. if not (0 < n < 4000):
  24. raise NameError('Out of range')
  25. print(int_to_roman(n))
  26. except NameError as name:
  27. sys.exit(name)
  28. except ValueError as exception:
  29. sys.exit(exception)
  30. except Exception as exception:
  31. print(exception.__class__)
Add Comment
Please, Sign In to add comment