Okay I am having trouble changing the inputted roman numeral into arabic number so I can add them up what am I doing wrong?
I know I still have a lot to go but I cannot even get the first steps right.

I am new to python.... so the hard coding I have seen so far is not helping and most coding is from Arabic to roman but i need from Roman to Arabic.

from string import *

"I" == 1
"V" == 5
"X" == 10
"L" == 50
"C" == 100
"D" == 500
"M" == 1000


roman = upper(raw_input("Enter the roman numeral to convert to arabic: "))

"I" == 1
"V" == 5
"X" == 10
"L" == 50
"C" == 100
"D" == 500
"M" == 1000

if roman[0] > roman[1]:
    arabic = roman[0] + roman[1]
    

print "The roman numeral",roman, "Is equal to",arabic

Dani AI

Generated

You are close. Two gotchas are tripping you up: (1) you are comparing characters like 'I' and 'V' instead of their numeric values, and (2) indexing with roman[0] and roman[1] will break for a 1-letter input. Building on ’s dict idea and ’s subtraction note, a simple, safe approach is to scan from right to left. If a symbol is smaller than the one to its right, subtract it; otherwise add it. Compare numbers, not letters, and do a little validation so junk like IL is rejected.

def roman_to_int(s):
    vals = dict(zip('IVXLCDM', (1,5,10,50,100,500,1000)))
    allowed = {('I','V'),('I','X'),('X','L'),('X','C'),('C','D'),('C','M')}
    s = s.strip().upper()
    if not s: raise ValueError('empty numeral')
    total = 0; prev = 0; last = ''; reps = 0
    for ch in reversed(s):
        if ch not in vals: raise ValueError('invalid char: %s' % ch)
        v = vals[ch]
        if v < prev:
            if (ch, last) not in allowed: raise ValueError('bad pair: %s%s' % (ch, last))
            total -= v; reps = 0
        else:
            total += v
            if ch == last:
                reps += 1
                if ch in 'VLD' or reps >= 3: raise ValueError('too many %s' % ch)
            else:
                reps = 0
        prev, last = v, ch
    return total

# Python 2 usage:
r = raw_input('Enter the Roman numeral: ')
try:
    print '%s = %d' % (r.upper(), roman_to_int(r))
except ValueError as e:
    print 'Error:', e

Notes:

  • Drop from string import *; use s.upper() directly.
  • Do not write "I" == 1; use the mapping above and always add/subtract integers.
  • If you still see a string, you are printing strings, not numbers. Format with %d (Py2) or print('{} = {}'.format(...)) in Py3.

Recommended Answers

All 5 Replies

To start, '==' is not how you assign a value to a variable. Use '=' for that, as '==' checks for equality. Change those first few lines that have that (the ones assigning the letter to decimal values). And, you can't assign a int value to a str value. You need to use a variable, like this:

valueI = 1
valueV = 5

A better way of doing this is using a dictionary:

mydict = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }
# so, mydict['I'] contains the value of 1, and so forth.

Also, 'upper()' is built-in to the str object, so you need to call it by saying

roman = raw_input("Enter the roman numeral to convert to arabic: ").upper()

Try making those changes and then proceeding with the code. Good luck!

Just being my pedantic self, you are changing inputted Roman Numerals to Hindi numbers- the Arabs borrowed the numbers from India (in Arabic they are known as 'Indian numbers' - arqa-m hindiyyah)

Just being my pedantic self, you are changing inputted Roman Numerals to Hindi numbers- the Arabs borrowed the numbers from India (in Arabic they are known as 'Indian numbers' - arqa-m hindiyyah)

I keep trying and trying but I just cannot get the answer to spit out in integers instead of a string...what am i doing wrong>??

Well, int(arabic) should return the numeric value of the str arabic. If that's any help.

Do not forget that anywhere in the string that if a numeral is smaller than the one that comes next, it is subtracted from the the one that comes next:

#
if roman[0] >= roman[1]:
#
arabic = roman[0] + roman[1]
Else
arabic = roman[1] - roman[0]

Or something like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.