Skip to content
Advertisement

How to convert Python Integer to Roman Numeral and vice-versa? [closed]

I am trying to write a Python program that accepts integer as input and displays the equivalent roman numeral and vice-versa. The program should be written in OOP. I have right now is the structure of the class and its methods but I can’t execute the conversion, does anyone know a good way to use OOP to my problem? It’s an assigned homework as part of my learning of scientific computing with Python. I am open to other good approaches you may have that follows and satisfies the given test cases below as long as it is in OOP.

Important Requirements:

  • MAX VALUE IS 5000, should only accept whole number value. provide the necessary error handling.

  • The conversion should accept uppercase and lowercase input.

  • After the output, the program should return to the menu choices.

See Roman Numerals Table

JavaScript

Advertisement

Answer

Here’s my take on an OOP approach to this.

We have a class Number which can be constructed in one of 3 ways. Either with a plain int, a string representation of an int or a Roman numeral.

The internal representation of the value is converted to a plain int.

Users can use the class’s asInteger() and asRoman() functions to retrieve the class’s value as an int or as a Roman numeral. These methods are impervious to the way the class was constructed.

Users can perform addition or subtraction using plain int or other instances of the Number class.

Examples of use follow the code:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement