1
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.

3
  • n[1] will fail with that error if there isn't an index of at least [1]. If you add a print(n) at the start of the for loop you'll see that n is only one character long (and therefore doesn't have an index 1) when you get this exception. Commented Apr 19, 2016 at 13:58
  • @dimo414 Why doesn't it? I appended it so that when the for loops runs through my list(x) the "n" value will hold a 2 character string as it runs through the loops. Commented Apr 19, 2016 at 14:03
  • @user3882522 As I show in my answer that doesn't really work because you are doing too many iterations when building x and you end up adding empty strings to the end of the list. Commented Apr 19, 2016 at 14:06

3 Answers 3

2

Well n is an element of x. The IndexError on the line n == n[0] + n[1] means that a certain n has length less than 2.

You added an word = word + 's' to probably guard against having one character elements in x but it doesn't really work.

If you look at how you build the x list you do:

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]) 

So in your example you start with x = [] and word = 'XXX'. Then you add an s to obtain word = 'XXXs'.

The loop over i does the following:

  • i=0 so x.append(word[0:2]); a = a+2; b = b+2 so that x = ['XX'] and a=2 and b=4.

  • i=1 so x.append(word[2:4]); a = a+2; b = b+2 so that x = ['XX', 'Xs'] and a=4 and b=6.

  • i=2 so x.append(word[4:6]); a = a+2; b = b+2 so that x = ['XX', 'Xs', ''] and a=6 and b=8.
  • i=3 so x.append(word[6:8]); a = a+2; b = b+2 so that x = ['XX', 'Xs', '', ''] and a=8 and b=10.

And here you see that n can be the empty string, which means when doing n == n[0] + n[1] you end up with an IndexError.

I believe you wanted to group the characters two by two, but then the i should use a step of 2:

for i in range(0, len(word), 2):
    x.append(word[i:i+2])

In this way i is 0, then 2, then 4 etc


By the way: once you have fixed this the condition n == n[0] + n[1] seems pretty odd, because if n is a two character string (as it should be if you fix the code) then the condition will always be true. What are you trying to really do here?

Sign up to request clarification or add additional context in comments.

4 Comments

now i remember, i was checking if the groups of two where the same. so if i were to put in "xx" then it would hold a value of true and multiply the corresponding value by 2 so in that case it would be 20. at least that's what i was trying to do..
I have a little bit more of a problems with my code, care to help?
@user3882522 Then you wanted to check n[0] == n[1]. If you have an other problem you should try to solve it yourself and eventually open a new question about it.
I have been trying for a whole lol. I'll open a new question about it, I just hope you're only before i get down voted to oblivion.. this site isn't very friendly to new users/beginner questions even if they're valid.
1

This is the culprit:

for i in range(0,len(word)):
     x.append(word[a:b])
     a = a + 2
     b = b + 2

At the end of this loop, x will be ['XX', 'Xs', '', '']. Since you are grouping the characters in groups of two, the total number of groups will be half the length of the string. So just halve the number of iterations with range(0,len(word)/2) or range(0,len(word),2)

Comments

1

You have a problem with your first for loop that goes farther than expected, affecting an empty string to x[i]. It should probably be : for i in range(int(len(word)/2)):

Then your second loop needs fixing too.

if n == rNum : is never realised since rNum is a one character string and x's length is 2. Try n == rNum+"s". n == n[0] + n[1] is always True for a string of 2 characters. You must mean n == rNum * 2

Also, the use of x += 1 is recommended instead of x = x + 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.