def romanToNum(word):
word = word.upper()
numeralList2 = list(zip(
[1000, 500, 100, 50, 10, 5, 1],
['M', 'D', 'C', 'L', 'X', 'V', 'I']
))
num = 0
x = []
a = 0
b = 2
if len(word) % 2 != 0:
word = word + "s"
for i in range(0,len(word)):
x.append(word[a:b])
a = a + 2
b = b + 2
print(x[i])
for n in x:
for nNum,rNum in numeralList2:
if n == rNum:
num = nNum + num
elif n == (n[0] + n[1]):
num = (nNum*2) + num
elif n[0] == rNum:
r1 = 0
r1 = nNum
elif n[1] == rNum:
r2 = 0
r2 = nNum
elif r1 < r2:
num = num + (r2 - r1)
elif r1 > r2:
num = num + (r1 + r2)
return num
romanToNum("xxx")
I am getting the following error:
elif n == (n[0] + n[1]):
IndexError: string index out of range
and it doesn't matter where I put that in the loop, it just wont recognize that n has an index value.
I also get this error: Traceback (most recent call last):
which points to when i call my function: romanToNum("xxx")
I'm not really sure what's going on because I added a print statement to where I'm appending my list and there is an index of at least [0] when I print it all out. Any help here?
I have looked through stack for similar questions but the solution for them is an indentation or because they had a negative index( [-1] ) or something along those lines but all my indentation is correct and my index's are all positive.
n[1]will fail with that error if there isn't an index of at least[1]. If you add aprint(n)at the start of the for loop you'll see thatnis only one character long (and therefore doesn't have an index1) when you get this exception.xand you end up adding empty strings to the end of the list.