Hey guys, I'm taking an intro level course on Python and i need to figure out how to get roman numerals to their integer values

so far i have this...

romaninput = raw_input("Enter the roman numeral to convert to Arabic: ")

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

#first letter of roman value must be bigger than next in order to add the values
if romaninput[0] > romaninput[1]:
    total = 
    print "The roman numeral", romaninput, "is equal to", total
    
#if letter is placed before a letter of greater values, subtract that amount

I know its not much, but I really don't know a lot about python.
Where I have "total =" I'm trying to get the roman values that the user inputs into the string to add up with a total of 3 repeating roman values. Ex: XXX = 30.

Any help would be appreciated,
Oakie

You would use a dictionary to convert.

convert_dic = {"I":1,
               "V":5,
               "X":10,
               "L": 50,
               "C ":100,
               "D": 500,
               "M": 1000 }

Also, this line should be "equal to or greater than".
if romaninput[0] > romaninput[1]:

Better yet, use a for loop to iterate through all of the roman numerals. Sorry if this is harsh, but if the above code is all you have, you'd should start reading the text book immediately and find out
1. How to enter the roman numeral from the terminal
2. Use a for() loop to examine each letter
3. How a dictionary works

Okay, I tried it another way... but I've ran into a slight problem. I couldn't figure out how to do it with the dictionary.

import string

#user inputs roman numerals
Input = string.upper(raw_input("Please enter a roman numeral to convert: "))

#variables assigned to roman numerals and then are counted from input
I = string.count(Input, "I")
V = string.count(Input, "V")
X = string.count(Input, "X")
L = string.count(Input, "L")
C = string.count(Input, "C")
D = string.count(Input, "D")
M = string.count(Input, "M")

#error statement for invalid number of roman numerals
if I > 3 or V > 2 or X > 3 or L > 3 or C > 3 or D > 3 or M > 3:
    print "Error cannot have more than 3",Input[0],"'s."
else:
    if I < 4:
        equation = I * 1 + V * 5 + X * 10 + L * 50 + C * 100 + D * 500 + M * 1000
        print "The roman numeral", Input, "is equal to", equation

When i input roman numerals like "IV" it gives me the wrong number "6". What can i do to solve this? I understand it is because of the way i did the equation. Is there another way to do it?

This article has been dead for over six months. Start a new discussion instead.