Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Roman Numerals to integers in python

This is now my current code after what user2486 said.

def romanMap():
    map=(("M",  1000),("CM", 900),("D",  500),("CD", 400),("C",  100),("XC", 90),("L",  50),("XL", 40),("X",  10),("IX", 9),("V",  5),("V", 4),("I",  1))
    return map
firstNum=ns([0])
secondNum=ns([1])
def main():
    ns=str(input("Enter a roman numeral"))
    total=0
    result=0
    while ns:
        firstNum=(romanMap(ns[0]))
         secondNum=(romanMap(ns[1]) 
        if firstNum is len(ns)>1 or secondNum-1:
                        total=total+firstNum
            ns=ns[1:]
        else:
                        total=total+ns[1]-ns[0]
            ns=ns[2:]
      print (total)
main()

I am getting this error with while ns: UnboundLocalError: local variable 'ns' referenced before assignment

like image 616
Jake Avatar asked Oct 11 '13 00:10

Jake


People also ask

How do you find Roman numerals in Python?

Numerals should follow a precise order: [M] 1000 / [D] 500 / [C] 100 / [L] 50 / [X] 10 / [V] 5 / [I] 1. A numeral cannot repeat more than 3 times, it then uses a pair. The following pairs are allowed: [CM] 900 / [CD] 400 / [XC] 90 / [XL] 40 / [IX] 9 / [IV] 4.

How do you convert integers to numbers in Python?

Number Type ConversionType int(x) to convert x to a plain integer. Type long(x) to convert x to a long integer. Type float(x) to convert x to a floating-point number. Type complex(x) to convert x to a complex number with real part x and imaginary part zero.


1 Answers

No need to reinvent the wheel (unless you want to). Python once came with a converter (so you can go to the Python 3.4.1 source code and grab the module at this location: /Python-3.4.1/Doc/tools/roman.py; or perhaps install it with pip as someone in the comments said here; I haven't verified the pip version; anyway, then you can do):

import roman;
n=roman.fromRoman("X"); #n becomes 10

If you need it for numbers 5000 and above, you'll need to write a new function, though, and maybe make your own font to represent the lines over the roman numerals. (It will only work with some numbers, at that. Stopping at 4999 is a really good idea.)

To convert to roman numerals, use roman.toRoman(myInt).

Alternatively (for converting to Roman numerals only), you can do this in Python 3.9.2 (which I only partially understand due to a lack of documentation; so, all my arguments probably aren't right; but, it seems to work; formatter is depreciated anyway; so, don't expect it to stay around a long time):

import formatter
a=formatter.AbstractFormatter("I don't know what I'm supposed to put here, but it doesn't seem to matter for our purposes.")
roman_numeral=a.format_roman(case="I", counter=5) #Case doesn't seem to matter, either.
#roman_numeral now equals "V"

Someone else actually linked to the same source code the roman module uses in one of the comments above, but I don't believe they mentioned that it actually comes with Python. It doesn't seem to come with Python anymore, but it did in version 3.4.1.

like image 104
Brōtsyorfuzthrāx Avatar answered Sep 30 '22 11:09

Brōtsyorfuzthrāx