Hello All,
I am trying to convert Roman number to Integer (III-->3) and to do that I need to compare characters one by one in a dictionary. How do I not get this error?
If this was a string I can do where S[i]=s[i+1], Tried to do same in the dictionary but it is throwing me an error.
def prit(num,count):
i=0
while(i<len(num)):
if(len(num)==1):
count = count + dict1[num[0]]
if(len(num)==0):
print(count)
exit()
else:
if(dict1[num[i]]>=dict1[num[i+1]]):
count = count + dict1[num[i]]
i=i+1
else:
count = dict1[num[i+1]]-dict1[num[i]]+count
i=i+2
return count
num="LVIII"
count=0
dict1 = {"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
count=prit(num,count)
print(count)
I am trying to convert Roman number to Integer (III-->3) and to do that I need to compare characters one by one in a dictionary. How do I not get this error?
If this was a string I can do where S[i]=s[i+1], Tried to do same in the dictionary but it is throwing me an error.
def prit(num,count):
i=0
while(i<len(num)):
if(len(num)==1):
count = count + dict1[num[0]]
if(len(num)==0):
print(count)
exit()
else:
if(dict1[num[i]]>=dict1[num[i+1]]):
count = count + dict1[num[i]]
i=i+1
else:
count = dict1[num[i+1]]-dict1[num[i]]+count
i=i+2
return count
num="LVIII"
count=0
dict1 = {"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
count=prit(num,count)
print(count)