class Solution: def romanToInt(self, s: str) -> int: num = 0 for i in range(len(s)): if s[i] == "I": try: if s[i+1] == "V" or s[i+1] == "X": num -= 1 continue except: num += 1 continue else: num += 1 continue elif s[i] == "X": try: if s[i+1] == "L" or s[i+1] == "C": num -= 10 continue except: num += 10 continue else: num += 10 continue elif s[i] == "C": try: if s[i+1] == "D" or s[i+1] == "M": num -= 100 continue except: num += 100 continue else: num += 100 continue elif s[i] == "V": num += 5 continue elif s[i] == "L": num += 50 continue elif s[i] == "D": num += 500 continue else: num += 1000 continue return num This is obviously not the most optimal approach, but I want to understand what's wrong with it... I know this is wrong because here were the 3 testcases from Leetcode
"III" --> 3 worked "LVIII" ---> 58 worked
BUT "MCMXCIV" --> 1884 expected 1984
This code basically gives each iteration a specific amount that will be added/substracted from the totalsum. But somehow 100 was deducted and I dont get how...
elsecorresponds to thetry, not theif, so it doesn't run. (Also, the expected result should be 1994, not 1984.)