I tried to create a function which would convert a roman numeral into an integer.
def romanToInt(s):
d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
d2 = {'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900}
keylist = list(d2.keys())
valuelist = list(d2.values())
count = 0
st = ''
if 'IV' and 'IX' and 'XL' and 'XC' and 'CD' and 'CM' not in s:
for letter in s:
count = count + d[letter]
else:
for roman in keylist:
if roman in s:
s = s.replace(roman,'')
count = count + valuelist[keylist.index(roman)]
for letter in s:
count = count + d[letter]
return count
When I tested the function against some test cases like "MCMXCIV", the function correctly interpreted it as 1994. However, when I texted the function against 'IV', it returned 6 instead of 4, which leads me to believe that it returns True for the scenario 'if 'IV' and 'IX' and 'XL' and 'XC' and 'CD' and 'CM' not in s:'.
I have no idea why my code is wrong, isn't 'IV' in 'IV'? Sorry if this is a dumb question, I just don't know why it isn't working.