-2

Not quite sure what I'm doing wrong. Using Python 3.2 Get the issue: TypeError: list indices must be integers, not list

Here's my code.

print('Please input your first Roman Numeral.')
ri1 = input()

def romantointeger(x):
conversion = [['M',1000],['CM',900],['D',500],['CD',400],['C',100],['XC',90],['L',50],['XL',40],['X',10]]     #creates Roman numer values
retint = 0 #creates the variable that will eventually be returned as the final value
for pair in conversion:
        cont = True #makes it continue for a pair
        while cont:
            if len(x) >= len(pair): #checks the length to see if it is greater than a pair of [0]
                if x[0:len(pair)]: 
                    retint += conversion[pair]
                    string = string[len(pair):]
                else:
                    cont = False
            else:
                cont = False
return retint

romantointeger(ri1)

3 Answers3

1
conversion ={'M':1000,'CM':900,'D':500,'CD':400,'C':100,'XC':90,'L':50,'XL':40,'X':10,}     
for pair in conversion:

pair is going to iterate over the dictionary's KEYS.

Try

for pair in conversion.items():

or even better:

for key, value in conversion.items():
Eric Levieil
  • 3,554
  • 2
  • 13
  • 18
0

conversion is a dictionary. That means looping through it for pair in conversion as so will assign each key of the dictionary to pair.

That means that on: retint += pair[1] should be retint += conversion[pair]

And any time you had pair[0] should just be pair.

However, this is still not going to work as you'd want (probably) as a dictionary is unsorted (your order is not going to necessarily be the same as what you entered. meaning your code will not work as you expect (as the input will be a string that is "sorted" such that the largest values come first).

A demonstration of this:

for key in conversion:
    print key

gave me:

X
C
XC
CD
D
XL
CM
L
M

Definitely not what you want!

What you want instead is to define conversion as a list of lists such that each element is the roman numeral and its value. So:

conversion = ['M',1000],['CM',900],['D',500],['CD',400],['C',100],['XC',90],['L',50],['XL',40],['X',10]]

You can leave the rest of the code alone and it'll work as you want as now pair[0] would be defined to the roman numeral code and pair[1] would be its numeric value (fixing your TypeError!). This list will always be in that exact order as well.

MasterOdin
  • 7,117
  • 1
  • 20
  • 35
  • Hello, thank you for the help, however I'm still having issues. Here is the code and issue. The original code is updated with the new one. Any help is much appreciated. Thanks! The new error is: TypeError: list indices must be integers, not list – Max Goldstein May 08 '15 at 21:12
0
class Solution(object):
def romanToInt(self, s):
    """
    :type s: str
    :rtype: int
    """
    single = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    num  = 0
    flag = -1

    if s in single:
        num += single[s]
        return num
    elif s not in single:
        for i in range(len(s)):
            if i == flag:
                pass
            elif i != flag:
                if i <= len(s)-2:
                    if single[s[i]] < single[s[i+1]]:
                        num += single[s[i+1]]-single[s[i]]
                        flag = i+1
                    else:
                        num += single[s[i]]
                else:
                    num += single[s[i]]
        return num
  • actually I am just a noob in python-learning and the answer released by me was just the key of an problem in leetcode. :) – Yunqing Zhao Jul 12 '18 at 13:36
  • You don't have to be a master to be able to try to help people. But some code/thoughts explanation would add much more value, as it is now. – Andrei Suvorkov Jul 12 '18 at 13:38
  • my method comes from the key word: Using Dictionary. I thinkt the important point lies in the 'flag'. once the programme meets the Roman being not single, the program will pass the number and match the next one. another important is the final words, i mean 'if i <= len(s)- 2' , it will judge wheather we should end the process. – Yunqing Zhao Jul 12 '18 at 13:42