def romanToInt(self, s: str) -> int:
num = 0
lst = ["I","V","X","L","C","D","M"]
dict = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
for i in range(len(s)):
if lst.index(s[i]) >= lst.index(s[i+1]) or i == len(s)-1:
num = num + dict[s[i]]
else:
num = num - dict[s[i]]
return num
this is my code for converting roman numeral to integers
the program is triggering this error
IndexError: string index out of range
for line 6
The error is because you are trying to access
s[i+1]in the loop wheniis equal tolen(s)-1. At that point,s[i+1]goes out of the string's bounds, resulting in an IndexError.