A.D A.D - 29 days ago 11
Python Question

List["M","C"] to -> MC

im pretty new to programming and trying to learn Python.
I tried to write a simple code which transforms an integer into roman numerals.
I´m searching for a method to transofrm the list I created into something like
MCCVI.
For now its enough if the integer exp. 1523 is written like MDXXIII.
Here is my code:

a= int(input("Geben sie eine Zahl ein:"))
M=1000
D=500
C=100
L=50
X=10
V=5
I=1
liste= ["M","D","C","L","X","V","I"]
i=0
erg=[]

while i < len(liste):

while a > eval(liste[i]):
a = a- eval(liste[i])
erg += liste[i]

i+=1

print(erg)

Answer
a= int(input("Geben sie eine Zahl ein:"))


roman_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'),(50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]



roman_num = ""
while a > 0:
    for i, r in roman_map:
        while a >= i:
            roman_num += r

            a -= i

print roman
Comments