Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in PYTHON to compute the roman numeral representation of integer

ID: 3669828 • Letter: W

Question

Write a program in PYTHON to compute the roman numeral representation of integers.

Given an integer larger than 0 and less than 4000, convert it to roman numerals. You can find the rules for this here, or simply search for it online.

There are many ways to do this, some more efficient than others. If it works and you wrote it yourself that’s good enough.

If the integer they provide is not in the right range, print “Input must be between 1 and 3999”

An example run of the program might look like:

Enter an integer: 1997
In roman numerals, 1997 is MCMXCVII

Another run might look like:

Enter an integer: 5820
Input must be between 1 and 3999

Explanation / Answer

def int_to_roman(number):
   if 0 < number < 4000:
      roman = ""
      roms = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
      ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,   5, 4,   1)
  
      for i in range(len(ints)):
         count = int(number / ints[i])
         number -= ints[i] * count
         roman += roms[i] * count
      return roman
   else :
      print "Input must be between 1 and 3999"
      exit()


number = int(input("Enter an interger"))
print
print int_to_roman(number)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote